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).
这道题的关键是由正整数组成的数组,这样才能保证累加的数组是递增的,才能使用双指针或者二分法。
解法1: 双指针,滑动窗口。
解法2: 二分法
Java: moving window
public class Solution { public int minSubArrayLen(int s, int[] nums) { int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE; while (j < nums.length) { while (sum < s && j < nums.length) sum += nums[j++]; if(sum>=s){ while (sum >= s && i < j) sum -= nums[i++]; min = Math.min(min, j - i + 1); } } return min == Integer.MAX_VALUE ? 0 : min; } }
Java: BS
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; } }
Java: BS
public class Solution { public int minSubArrayLen(int s, int[] nums) { int sum = 0, min = Integer.MAX_VALUE; int[] sums = new int[nums.length]; for (int i = 0; i < nums.length; i++) sums[i] = nums[i] + (i == 0 ? 0 : sums[i - 1]); for (int i = 0; i < nums.length; i++) { int j = findWindowEnd(i, sums, s); if (j == nums.length) break; min = Math.min(j - i + 1, min); } return min == Integer.MAX_VALUE ? 0 : min; } private int findWindowEnd(int start, int[] sums, int s) { int i = start, j = sums.length - 1, offset = start == 0 ? 0 : sums[start - 1]; while (i <= j) { int m = (i + j) / 2; int sum = sums[m] - offset; if (sum >= s) j = m - 1; else i = m + 1; } return i; }
Java:
public int minSubArrayLen(int s, int[] a) { if (a == null || a.length == 0) return 0; int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE; while (j < a.length) { sum += a[j++]; while (sum >= s) { min = Math.min(min, j - i); sum -= a[i++]; } } return min == Integer.MAX_VALUE ? 0 : min; }
Java:
public class Solution { public int minSubArrayLen(int s, int[] nums) { return solveNLogN(s, nums); } private int solveN(int s, int[] nums) { int start = 0, end = 0, sum = 0, minLen = Integer.MAX_VALUE; while (end < nums.length) { while (end < nums.length && sum < s) sum += nums[end++]; if (sum < s) break; while (start < end && sum >= s) sum -= nums[start++]; if (end - start + 1 < minLen) minLen = end - start + 1; } return minLen == Integer.MAX_VALUE ? 0 : minLen; } private int solveNLogN(int s, int[] nums) { int[] sums = new int[nums.length + 1]; for (int i = 1; i < sums.length; i++) sums[i] = sums[i - 1] + nums[i - 1]; int minLen = Integer.MAX_VALUE; for (int i = 0; i < sums.length; i++) { int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums); if (end == sums.length) break; if (end - i < minLen) minLen = end - i; } return minLen == Integer.MAX_VALUE ? 0 : minLen; } private int binarySearch(int lo, int hi, int key, int[] sums) { while (lo <= hi) { int mid = (lo + hi) / 2; if (sums[mid] >= key){ hi = mid - 1; } else { lo = mid + 1; } } return lo; } }
Python:
# Sliding window solution. class Solution: # @param {integer} s # @param {integer[]} nums # @return {integer} def minSubArrayLen(self, s, nums): start = 0 sum = 0 min_size = float("inf") for i in xrange(len(nums)): sum += nums[i] while sum >= s: min_size = min(min_size, i - start + 1) sum -= nums[start] start += 1 return min_size if min_size != float("inf") else 0
Python:
# Time: O(nlogn) # Space: O(n) # Binary search solution. class Solution2: # @param {integer} s # @param {integer[]} nums # @return {integer} def minSubArrayLen(self, s, nums): min_size = float("inf") sum_from_start = [n for n in nums] for i in xrange(len(sum_from_start) - 1): sum_from_start[i + 1] += sum_from_start[i] for i in xrange(len(sum_from_start)): end = self.binarySearch(lambda x, y: x <= y, sum_from_start, \ i, len(sum_from_start), \ sum_from_start[i] - nums[i] + s) if end < len(sum_from_start): min_size = min(min_size, end - i + 1) return min_size if min_size != float("inf") else 0 def binarySearch(self, compare, A, start, end, target): while start < end: mid = start + (end - start) / 2 if compare(target, A[mid]): end = mid else: start = mid + 1 return start
C++:
/ O(n) class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { if (nums.empty()) return 0; int left = 0, right = 0, sum = 0, len = nums.size(), res = len + 1; while (right < len) { while (sum < s && right < len) { sum += nums[right++]; } while (sum >= s) { res = min(res, right - left); sum -= nums[left++]; } } return res == len + 1 ? 0 : res; } };
C++:
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX, left = 0, sum = 0; for (int i = 0; i < nums.size(); ++i) { sum += nums[i]; while (left <= i && sum >= s) { res = min(res, i - left + 1); sum -= nums[left++]; } } return res == INT_MAX ? 0 : res; } };
C++: 二分法
// O(nlgn) class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int len = nums.size(), sums[len + 1] = {0}, res = len + 1; for (int i = 1; i < len + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; for (int i = 0; i < len + 1; ++i) { int right = searchRight(i + 1, len, sums[i] + s, sums); if (right == len + 1) break; if (res > right - i) res = right - i; } return res == len + 1 ? 0 : res; } int searchRight(int left, int right, int key, int sums[]) { while (left <= right) { int mid = (left + right) / 2; if (sums[mid] >= key) right = mid - 1; else left = mid + 1; } return left; } };
C++:
class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int res = INT_MAX, n = nums.size(); vector<int> sums(n + 1, 0); for (int i = 1; i < n + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1]; for (int i = 0; i < n; ++i) { int left = i + 1, right = n, t = sums[i] + s; while (left <= right) { int mid = left + (right - left) / 2; if (sums[mid] < t) left = mid + 1; else right = mid - 1; } if (left == n + 1) break; res = min(res, left - i); } return res == INT_MAX ? 0 : res; } };
类似题目:
[LeetCode] 53. Maximum Subarray 最大子数组
[LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
[LeetCode] 560. Subarray Sum Equals K 子数组和为K
All LeetCode Questions List 题目汇总