C# two pointer solution


  • 0
    H
    public int MinSubArrayLen(int s, int[] nums) {
            int i = 0, j = 0, minLen = int.MaxValue;
            int sum = 0;
            
            while(j < nums.Length) {
                while(j < nums.Length && sum < s) sum += nums[j++];
                while(i < j && sum >= s) {
                    minLen = Math.Min(minLen, j - i);
                    sum -= nums[i];
                    i++;
                }
            }
            
            return minLen == int.MaxValue? 0 : minLen;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.