1477. Find Two Non-overlapping Sub-arrays Each With Target Sum Medium
1/**
2 * [1477] Find Two Non-overlapping Sub-arrays Each With Target Sum
3 *
4 * You are given an array of integers arr and an integer target.
5 * You have to find two non-overlapping sub-arrays of arr each with a sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
6 * Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
7 *
8 * Example 1:
9 *
10 * Input: arr = [3,2,2,4,3], target = 3
11 * Output: 2
12 * Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
13 *
14 * Example 2:
15 *
16 * Input: arr = [7,3,4,7], target = 7
17 * Output: 2
18 * Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
19 *
20 * Example 3:
21 *
22 * Input: arr = [4,3,2,6,2,3,4], target = 6
23 * Output: -1
24 * Explanation: We have only one sub-array of sum = 6.
25 *
26 *
27 * Constraints:
28 *
29 * 1 <= arr.length <= 10^5
30 * 1 <= arr[i] <= 1000
31 * 1 <= target <= 10^8
32 *
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/
37// discuss: https://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42 pub fn min_sum_of_lengths(arr: Vec<i32>, target: i32) -> i32 {
43 0
44 }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn test_1477() {
55 }
56}
57
Back
© 2025 bowen.ge All Rights Reserved.