-
Notifications
You must be signed in to change notification settings - Fork 2
/
LeetCode209.java
42 lines (39 loc) · 1.12 KB
/
LeetCode209.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
35
36
37
38
39
40
41
42
package problems;
public class LeetCode209 {
public static int minSubArrayLen(int target, int[] nums) {
int left = 0, min = Integer.MAX_VALUE, sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
while (sum >= target) {
min = Math.min(min, i - left + 1);
sum -= nums[left++];
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
public static void main(String[] args) {
int[] nums = new int[]{2, 3, 1, 2, 4,3};
minSubArrayLen(7, nums);
}
}
class LeetCode209_1 {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
if(n == 0) {
return 0;
}
int ans = Integer.MAX_VALUE;
int start = 0, end = 0;
int sum = 0;
while (end < n) {
sum += nums[end];
while (sum >= target) {
ans = Math.min(ans, end - start + 1);
sum -= nums[start];
start++;
}
end++;
}
return ans == Integer.MAX_VALUE ? 0 : ans;
}
}