package com.fishercoder.solutions;

/**
 * Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.

 For example, given the array [2,3,1,2,4,3] and s = 7,
 the subarray [4,3] has the minimal length under the problem constraint.

 click to show more practice.

 More practice:
 If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
 */
public class _209 {

    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int i = 0;
        int j = 0;
        int min = Integer.MAX_VALUE;
        int sum = 0;
        while (j < nums.length) {
            sum += nums[j++];

            while (sum >= s) {
                min = Math.min(min, j - i);
                sum -= nums[i++];
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }

}

@@ -33,16 +33,53 @@
 s;%0A%0A/**%0A
+ * 209. Minimum Size Subarray Sum%0A *%0A
  * Given
@@ -136,16 +136,19 @@
 teger s,
+%0A *
  find th
@@ -210,16 +210,19 @@
 sum %E2%89%A5 s.
+%0A *
  If ther
@@ -472,16 +472,17 @@
 olution,
+%0A
  try cod
@@ -571,16 +571,56 @@
 _209 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -670,16 +670,20 @@
         
+   
+ 
 if (nums
@@ -722,24 +722,28 @@
             
+    
 return 0;%0A  
@@ -736,24 +736,28 @@
   return 0;%0A
+    
         %7D%0A  
@@ -758,24 +758,28 @@
   %7D%0A        
+    
 int i = 0;%0A 
@@ -781,24 +781,28 @@
  0;%0A        
+ 
+   
 int j = 0;%0A 
@@ -804,24 +804,28 @@
  0;%0A        
+  
+  
 int min = In
@@ -845,24 +845,28 @@
 UE;%0A        
+    
 int sum = 0;
@@ -870,24 +870,28 @@
  0;%0A        
+   
+ 
 while (j %3C n
@@ -900,24 +900,28 @@
 s.length) %7B%0A
+    
             
@@ -947,24 +947,28 @@
             
+    
 while (sum %3E
@@ -990,16 +990,20 @@
         
+ 
+   
 min = Ma
@@ -1034,24 +1034,28 @@
             
+    
 sum -= nums%5B
@@ -1072,26 +1072,33 @@
         
+  
-%7D%0A
+  %7D%0A   
         
 %7D%0A      
@@ -1081,34 +1081,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         return m
@@ -1151,13 +1151,22 @@
 in;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 209