LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)

题目描述

给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。

示例:

输入: s = 7, nums = [2,3,1,2,4,3]
输出: 2
解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。

解题思路

记录当前的连续子数组和,若大于等于s,则以当前子数组的最左端为基准向后遍历,若去掉此数后当前连续子数组和仍大于等于s,就把左端向右移动一位,否则更新当前的最小连续子数组长度。

代码

 1 class Solution {
 2 public:
 3     int minSubArrayLen(int s, vector<int>& nums) {
 4         int minCnt = INT_MAX, left = 0, right = 0, sum = 0;
 5         while(right < nums.size()){
 6             sum += nums[right];
 7             if(sum >= s){
 8                 while(left < right && sum - nums[left] >= s)
 9                     sum -= nums[left++];
10                 minCnt = min(minCnt, right - left + 1);
11             }
12             right++;
13         }
14         if(minCnt == INT_MAX) return 0;
15         return minCnt;
16     }
17 };

原文地址:https://www.cnblogs.com/wmx24/p/10456454.html

时间: 2024-10-08 19:11:58

LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)的相关文章

[Swift]LeetCode209. 长度最小的子数组 | 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

大于或等于给定值 长度最小的子数组 minimum size subarray sum [209]

leetcode 209 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] ha

【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] 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(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution 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, r

[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.长度最小的子数组 By Python

给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组. 进阶: 如果你已经完成了O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法. 思路 滑动窗口法,设立两个指针i,j,判断nums[i:j+1]跟s的大小关系来决定i,j的走向,

leetcode 209. 长度最小的子数组 java

题目: 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组. 进阶: 如果你已经完成了O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法. 解题: class Solution { public int minSubArrayLe

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