【刷题-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 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 暴力搜索
找出所有的$\sum_{k=i}^j a_k \geq s \(的子串,取长度\)j-i+1$最小的
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if(nums.size() == 0)return 0;
int ans = INT_MAX;
for(int i = 0; i < nums.size(); ++i){
int tmp_sum = 0;
for(int j = i; j < nums.size() && j - i <= ans; ++j){
tmp_sum += nums[j];
if(tmp_sum >= s)ans = min(ans, j-i+1);
}
}
return ans == INT_MAX ? 0 : ans;
}
};
Note :
- 在内层循环中,一定要加
j - i <= ans的判断条件,否则会超时 - 为了避免在内层循环中重复求和,可以先计算nums的前n项和,放到数组sum中
解法2 二分查找。前n项和数组sum一定是单调递增的,原问题可以转换为:
查找\(sum[i] + s\)在sum中第一次出现的位置\((i = 0, 1, 2, ..., nums.size())\),即找\(nums[i] + ... + nums[j] \geq s\)对应的最小长度
直接调用c++ stl中的lower_bound()函数
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if(nums.size() == 0)return 0;
vector<int>sum(nums.size() + 1, 0);
for(int i = 0; i < nums.size(); ++i)sum[i+1] = sum[i]+nums[i];
int ans = INT_MAX;
for(int i = 1; i <= nums.size(); ++i){
int to_find = s + sum[i-1];
auto bound = lower_bound(sum.begin(), sum.end(), to_find);
if(bound != sum.end())ans = min(ans, int(bound - sum.begin()) - i + 1);
}
return ans == INT_MAX ? 0 : ans;
}
};
解法3 one-pass。记录满足\(sum \geq s\)的子串的起始位置,在找到一个符合条件的子串后,不断收缩子串
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if(nums.size() == 0)return 0;
int ans = INT_MAX, left = 0, sum = 0;
for(int i = 0; i < nums.size(); ++i){
sum += nums[i];
while(sum >= s){
ans = min(ans, i - left + 1);
sum -= nums[left++];
}
}
return ans == INT_MAX ? 0 : ans;
}
};
【刷题-LeetCode】209. Minimum Size Subarray Sum的更多相关文章
- [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode 209. Minimum Size Subarray Sum (最短子数组之和)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 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 subarra ...
- 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 subarra ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 209. Minimum Size Subarray Sum(双指针)
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- [刷题] 209 Minimum Size Subarray Sum
要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3 ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
随机推荐
- LuoguP7505 「Wdsr-2.5」小小的埴轮兵团 题解
Content 给出一个范围为 \([-k,k]\) 的数轴,数轴上有 \(n\) 个点,第 \(i\) 个点的位置为 \(a_i\).有 \(m\) 次操作,有且仅有以下三种: 1 x:所有点往右移 ...
- SQL获取当天0点0分0秒和23点59分59秒方法
SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120)) select cast(convert(varchar(10),getdate( ...
- 总结Vue第一天:简单介绍、基本常用知识、辅助函数
总结Vue第一天:简单介绍.基本常用知识.辅助函数 中文官网:https://cn.vuejs.org/v2/guide/syntax.html 遇到不熟悉的可以先看一下官网,然后再看一下一些别人写的 ...
- RPA项目POC指南:概念、步骤与技巧
"为什么部署RPA前要进行POC?RPA不是开箱即用吗?" 其实,RPA的实施并非总是一帆风顺,"碰坑"在所难免. 据安永报告显示,30%至50%的初始RPA项 ...
- c++之别让异常逃离析构函数
关于 本文代码演示环境: VS2017. 代码写的够不规范,目的是为了缩短文章篇幅. 本文主要是为了加深印象,写了好多次的代码,还是忘记了这茬.... 之前上传到github的代码会慢慢改过来. 本文 ...
- 【LeetCode】LCP 07. 传递信息
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- 【LeetCode】638. Shopping Offers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯法 日期 题目地址:https://le ...
- 【剑指Offer】构建乘积数组 解题报告(Python)
[剑指Offer]构建乘积数组 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目 ...
- BZOJ 1857: [Scoi2010]传送带(三分套三分)
Time Limit: 1 Sec Memory Limit: 64 MB Submit: 2549 Solved: 1370 [Submit][Status][Discuss] Descriptio ...