-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0209.java
More file actions
31 lines (27 loc) · 891 Bytes
/
Copy pathLeetCode0209.java
File metadata and controls
31 lines (27 loc) · 891 Bytes
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
/* Minimum Size Subarray Sum
* Input: s = 7, nums = [2,3,1,2,4,3]
* Output: 2
* Explanation: the subarray [4,3] has the minimal length under the problem constraint.
* */
public class LeetCode0209 {
public static void main(String[] args) {
int s = 7;
int[] nums = {2,3,1,2,4,3};
System.out.println(minSubArrayLen(s, nums));
}
public static int minSubArrayLen(int s, int[] nums) {
if (nums.length == 0)
return 0;
int minLen = Integer.MAX_VALUE;
int left = 0;
int subSum = 0;
for (int i = 0; i < nums.length; i++) {
subSum += nums[i];
while (subSum >= s) {
minLen = Math.min(minLen, i + 1 - left);
subSum -= nums[left++];
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
}