-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy path209. Minimum Size Subarray Sum
More file actions
38 lines (33 loc) · 1.06 KB
/
209. Minimum Size Subarray Sum
File metadata and controls
38 lines (33 loc) · 1.06 KB
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
class Solution {
public int minSubArrayLen(int s, int[] nums) {
int globalFewest = Integer.MAX_VALUE;
int interFewest = 0;
int lastIndex = 0;
int subArrayTotal = 0;
boolean foundMin = false;
for(int i = 0; i < nums.length; i++){
subArrayTotal += nums[i];
interFewest++;
//If you've achieved maximum, keep dropping off numbers
if(subArrayTotal >= s){
foundMin = true;
if(interFewest < globalFewest){
globalFewest = interFewest;
}
while(subArrayTotal >= s){
subArrayTotal -= nums[lastIndex++];
interFewest--;
if(s <= subArrayTotal && interFewest < globalFewest){
globalFewest = interFewest;
}
}
}
}
if(foundMin){
return globalFewest;
}
else{
return 0;
}
}
}