【Leetcode】Minimum Size Subarray Sum

题目链接:https://leetcode.com/problems/minimum-size-subarray-sum/

题目:

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn‘t one, return
0 instead.

For example, given the array [2,3,1,2,4,3] and s
= 7
,

the subarray [4,3] has the minimal length under the problem constraint.

click to show more practice.

More practice:

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

思路:

用两个游标表示当前范围,若满足条件则更新最小长度,否则游标做出相应调整。

算法

	public int minSubArrayLen(int s, int[] nums) {
		if (nums.length == 0)
			return 0;
		int minLen = Integer.MAX_VALUE, start = 0, end = 0;
		int sum = 0;
		while (end < nums.length) {
			while (sum < s && end < nums.length) {
				sum += nums[end++];//若不满足 则扩大范围
			}
			while (sum >= s && start < end) {
				minLen = Math.min(minLen, end - start);
				sum -= nums[start++];//当满足条件后    start增大看是否还存在更小长度
			}
		}
		if (minLen == Integer.MAX_VALUE) {
			minLen = 0;
		}
		return minLen;
	}
时间: 2024-10-14 10:19:25

【Leetcode】Minimum Size Subarray Sum的相关文章

【leetcode】Minimum Size Subarray Sum(middle)

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

[LeetCode] 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 subarr

LeetCode 209. Minimum Size Subarray Sum (O(n)实现)

动态规划: len[i]: - 若存在begin使得sum(nums.begin()+begin, nums.begin()+i+1)>=s且sum(nums.begin()+begin-1, nums.begin()+i+1)<s,那么len[i] = i - begin + 1 (这串数字的长度) - 反之,len[i] = len[i - 1] + 1; sum[i]: - 若存在上述begin, sum = sum(nums.begin()+begin, nums.begin()+i+

LeetCode 209. Minimum Size Subarray Sum(DP)

题目 题意:求一个数组里最短的连续子序列的和大于等于s的长度 题解:可以用动态规划,我就是用动态规划过的,但是确实不是最简单的解法,看了题解最简单的是双指针, 双指针 class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int sum=0; int left=0; int ans=INT_MAX; for(int i=0;i<nums.size();i++) { sum+=nums[i];

【LeetCode 209】Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

Leetcode 209. Minimum Size Subarray Sum

思路一: 本题nums里面的数由于都是正数,所以可以用两个指针 start 和 end = 0.end++向后寻找,直到 start和end之间的sum>=s.例如 nums = [2, 3, 1, 2, 4, 3], s = 7.这一步结束时找到了start = 0 的最短的满足条件的subarray. 显然当前的这个解可能不是以end = 3的最优解,所以start++,直到total < 7. 这时我们又需要end++,直到total>=s来寻找以start = 1开头的最短sub

Java for LeetCode 209 Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal

LeetCode 209 Minimum Size Subarray Sum 最小子数组和问题

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguoussubarray 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 subarra

[刷题] LeetCode 209 Minimum Size Subarray Sum

要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的连续子数组 思路 暴力解法(n3) 滑动窗口() 1 class Solution{ 2 public: 3 int minSubArrayLen(int s, vector<int>& nums){ 4 int