Minimum Size Subarray Sum
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.
Discussion :
Discussion :
public int minSubArrayLen(int s, int[] nums) { int n = nums.length; int ans = Integer.MAX_VALUE; int left = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += nums[i]; while (sum >= s) { ans = Math.min(ans, i + 1 - left); sum -= nums[left++]; } } return (ans != Integer.MAX_VALUE) ? ans : 0; }
Comments
Post a Comment