Simple solution - Java - 4ms


  • -1
    A
    public class Solution 
    {
        public int minSubArrayLen(int s, int arr[]) 
        {
            int sum=0, ans=Integer.MAX_VALUE, i=0;
            Queue<Integer> queue = new LinkedList<Integer>();
            while(i<arr.length || s<=sum)
            {
                if(sum<s)
                {
                    sum += arr[i];
                    queue.add(arr[i++]);
                }
                else
                {
                    ans = ((ans<queue.size()) ? ans : queue.size());
                    sum -= queue.poll();
                }
            }
            if(ans==Integer.MAX_VALUE)
                return 0;
            return ans;
        }
    }

Log in to reply
 

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