-
Notifications
You must be signed in to change notification settings - Fork 65
/
Solution.java
34 lines (32 loc) · 996 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
30
31
32
33
34
package g0201_0300.s0209_minimum_size_subarray_sum;
// #Medium #Array #Binary_Search #Prefix_Sum #Sliding_Window #Algorithm_II_Day_5_Sliding_Window
// #Binary_Search_II_Day_1 #2022_06_28_Time_1_ms_(100.00%)_Space_50.1_MB_(11.60%)
public class Solution {
public int minSubArrayLen(int target, int[] nums) {
int i = 0;
int j = 0;
int sum = 0;
int min = Integer.MAX_VALUE;
while (j < nums.length) {
sum += nums[j];
if (sum >= target) {
while (i <= j) {
if (sum - nums[i] >= target) {
sum = sum - nums[i];
i++;
} else {
break;
}
}
if (j - i + 1 < min) {
min = j - i + 1;
}
}
j++;
}
if (min == Integer.MAX_VALUE) {
return 0;
}
return min;
}
}