1477. Find Two Non-overlapping Sub-arrays Each With Target Sum
</> Solution
/**
* Problem: LeetCode 1477 - Find Two Non-overlapping Sub-arrays Each With Target Sum
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Problem: LeetCode 1477 - Find Two Non-overlapping Sub-arrays Each With Target Sum
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int minSumOfLengths(int[] arr, int target) {
int n = arr.length;
int[] best = new int[n];
Arrays.fill(best, Integer.MAX_VALUE);
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0, ans = Integer.MAX_VALUE, curBest = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (map.containsKey(sum - target)) {
int j = map.get(sum - target);
int len = i - j;
if (j >= 0 && best[j] != Integer.MAX_VALUE) {
ans = Math.min(ans, len + best[j]);
}
curBest = Math.min(curBest, len);
}
best[i] = curBest;
map.put(sum, i);
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
}
"""
Problem: LeetCode 1477 - Find Two Non-overlapping Sub-arrays Each With Target Sum
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
best = [float('inf')] * n
prefix_map = {0: -1}
current_sum = 0
ans = float('inf')
cur_best = float('inf')
for i in range(n):
current_sum += arr[i]
if (current_sum - target) in prefix_map:
j = prefix_map[current_sum - target]
length = i - j
if j >= 0 and best[j] != float('inf'):
ans = min(ans, length + best[j])
cur_best = min(cur_best, length)
best[i] = cur_best
prefix_map[current_sum] = i
return -1 if ans == float('inf') else int(ans)
/**
* Problem: LeetCode 1477 - Find Two Non-overlapping Sub-arrays Each With Target Sum
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>
using namespace std;
/**
* Problem: LeetCode 1477 - Find Two Non-overlapping Sub-arrays Each With Target Sum
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int minSumOfLengths(vector<int>& arr, int target) {
int n = arr.size();
vector<int> best(n, INT_MAX);
unordered_map<int, int> prefixMap;
prefixMap[0] = -1;
int sum = 0;
int ans = INT_MAX;
int curBest = INT_MAX;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (prefixMap.count(sum - target)) {
int j = prefixMap[sum - target];
int len = i - j;
if (j >= 0 && best[j] != INT_MAX) {
ans = min(ans, len + best[j]);
}
curBest = min(curBest, len);
}
best[i] = curBest;
prefixMap[sum] = i;
}
return ans == INT_MAX ? -1 : ans;
}
};
/**
* Problem: LeetCode 1477 - Find Two Non-overlapping Sub-arrays Each With Target Sum
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var minSumOfLengths = function(arr, target) {
const n = arr.length;
const best = new Array(n).fill(Infinity);
const map = new Map();
map.set(0, -1);
let sum = 0;
let ans = Infinity;
let curBest = Infinity;
for (let i = 0; i < n; i++) {
sum += arr[i];
if (map.has(sum - target)) {
const j = map.get(sum - target);
const len = i - j;
if (j >= 0 && best[j] !== Infinity) {
ans = Math.min(ans, len + best[j]);
}
curBest = Math.min(curBest, len);
}
best[i] = curBest;
map.set(sum, i);
}
return ans === Infinity ? -1 : ans;
};