Java simple solution o(n) & 12 lines


  • 0
    D
     public static int minSubArrayLen(int s, int[] nums) {
        Deque<Integer> queue = new LinkedList<>();
        int minLength = Integer.MAX_VALUE;
        int sum = 0;
        for (int num : nums) {
            sum += num;
            queue.offer(num);
            if (sum >= s){
                while (sum - queue.peek() >= s) sum -= queue.poll();
                minLength = Math.min(minLength, queue.size());
            }
        }
       return sum < s ? 0 : minLength;
    }

  • 0
    A
    This post is deleted!

Log in to reply
 

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