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+1);

- 反之,sum = sum(nums.begin(), nums.begin()+i+1);

所求结果(函数返回值)即为:

min_element(len[i]); // 其中i满足sum[i] >= s

初始化:

len[0] = 1;

sum[0] = nums[0];

动态方程

参见代码中的for循环迭代。

时间复杂度分析

注意到,我们在循环外引入了一个变量begin. 时间复杂度分析与这个begin有关:

这有用到势能函数的概念,可以发现在整个函数中,begin向前移进不会超过nums.size(), 这是我在嵌套for的判断条件中没有显式约定的。因此,这个嵌套for中的判断总共被执行的次数不会超过2n次,每次i都移进一次begin, 第二次判断时不满足约束,终止for. 所以其在函数中的复杂度为O(n).

而关于i的for循环,显然也是O(n).

所以函数的时间复杂度为O(n).

代码:

class Solution
{
public:
    int minSubArrayLen(int s, vector<int>& nums)
    {
    	if (nums.empty())
    	{
    		return 0;
    	}

    	vector<int> sum(nums.size(), 0);
    	vector<int> len(nums.size(), 0);
    	sum[0] = nums[0];
    	len[0] = 1;
    	int min_len = sum[0]>=s? 1: INT_MAX;
    	int begin = 0;

    	for (size_t i = 1; i < nums.size(); ++ i)
    	{
 			sum[i] = sum[i-1] + nums[i];
    		if (sum[i] < s)
    		{
    			len[i] = len[i-1] + 1;
    		} else
    		{
    			// note that this 'begin' won't be larger than nums.size()
    			for (; sum[i] - nums[begin] >= s; sum[i] -= nums[begin], ++ begin) {}
    			len[i] = i - begin + 1;
    			min_len = min(min_len, len[i]);
    		}
    	}

    	return min_len==INT_MAX? 0: min_len;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-21 04:11:36

LeetCode 209. Minimum Size Subarray Sum (O(n)实现)的相关文章

[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(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

思路一: 本题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

【LeetCode】209. Minimum Size Subarray Sum

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 subar

LeetCode OJ 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】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