209. 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.
Example:
Input:s = 7, nums = [2,3,1,2,4,3]Output: 2 Explanation: the subarray[4,3]has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
class Solution { public int minSubArrayLen(int target, int[] nums) { int l = 0, cur = 0, res = Integer.MAX_VALUE; for(int r = 0; r < nums.length; r++) { cur += nums[r]; while(cur >= target) { res = Math.min(r - l + 1, res); cur -= nums[l++]; } } return res == Integer.MAX_VALUE ? 0 : res; } }
great or euqal than。。。sliding window
O(NLogN) - search if a window of size k exists that satisfy the condition public class Solution { public int minSubArrayLen(int s, int[] nums) { int i = 1, j = nums.length, min = 0; while (i <= j) { int mid = (i + j) / 2; if (windowExist(mid, nums, s)) { j = mid - 1; min = mid; } else i = mid + 1; } return min; } private boolean windowExist(int size, int[] nums, int s) { int sum = 0; for (int i = 0; i < nums.length; i++) { if (i >= size) sum -= nums[i - size]; sum += nums[i]; if (sum >= s) return true; } return false; } }

浙公网安备 33010602011771号