File tree 1 file changed +15
-14
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +15
-14
lines changed Original file line number Diff line number Diff line change 3
3
public class _209 {
4
4
5
5
public static class Solution1 {
6
- public int minSubArrayLen (int s , int [] nums ) {
7
- if (nums == null || nums .length == 0 ) {
8
- return 0 ;
9
- }
10
- int i = 0 ;
11
- int j = 0 ;
12
- int min = Integer .MAX_VALUE ;
6
+ /**
7
+ * A classic sliding window problem/solution.
8
+ */
9
+ public int minSubArrayLen (int target , int [] nums ) {
10
+ int left = 0 ;
11
+ int right = 0 ;
13
12
int sum = 0 ;
14
- while (j < nums .length ) {
15
- sum += nums [j ++];
16
-
17
- while (sum >= s ) {
18
- min = Math .min (min , j - i );
19
- sum -= nums [i ++];
13
+ int ans = Integer .MAX_VALUE ;
14
+ while (right < nums .length ) {
15
+ sum += nums [right ];
16
+ while (sum >= target ) {
17
+ ans = Math .min (ans , right - left + 1 );
18
+ sum -= nums [left ];
19
+ left ++;
20
20
}
21
+ right ++;
21
22
}
22
- return min == Integer .MAX_VALUE ? 0 : min ;
23
+ return ans == Integer .MAX_VALUE ? 0 : ans ;
23
24
}
24
25
}
25
26
}
You can’t perform that action at this time.
0 commit comments