forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
29 lines (29 loc) · 879 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int minSizeSubarray(int[] nums, int target) {
long s = Arrays.stream(nums).sum();
int n = nums.length;
int a = 0;
if (target > s) {
a = n * (target / (int) s);
target -= target / s * s;
}
if (target == s) {
return n;
}
Map<Long, Integer> pos = new HashMap<>();
pos.put(0L, -1);
long pre = 0;
int b = 1 << 30;
for (int i = 0; i < n; ++i) {
pre += nums[i];
if (pos.containsKey(pre - target)) {
b = Math.min(b, i - pos.get(pre - target));
}
if (pos.containsKey(pre - (s - target))) {
b = Math.min(b, n - (i - pos.get(pre - (s - target))));
}
pos.put(pre, i);
}
return b == 1 << 30 ? -1 : a + b;
}
}