LeetCode 209. Minimum Size Subarray Sum

Minimum Size Subarray Sum


public class Solution {
    public int MinSubArrayLen(int s, int[] nums) {
        int len = int.MaxValue;
        int cur = 0;
        int left = 0;
        int right = -1;
        while(right < nums.Length-1){
            right += 1;
            cur += nums[right];
            bool moved = false;
            while(cur >= s) {
                cur -= nums[left];
                left += 1;
                moved = true;
            }
            if (moved) {
                left -= 1; 
                cur += nums[left]; 
            } 
            if(cur >= s) len = Math.Min(right-left+1, len);
        }
        if(len == int.MaxValue) len = 0;
        return len;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *