【刷题-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- ...
随机推荐
- 分布式文件系统fastdfs安装以及python调用
fastfds的安装和使用 一.所需依赖 操作系统:centos7.x(注意的是centos使用yum安装相关依赖) fastdfs:V6.06.tar.gz libfastcommon:V1.0.4 ...
- jquery Ajax 不执行回调函数success的原因
jquery Ajax 不执行回调函数success的原因: $.ajax({ type: "post", contentType: "application/json& ...
- WPF控件界面自适应
之前就听说WPF流式布局,顺滑的很.但由于专业只学习了winform,工作对界面的要求并不高一直没去玩它.目前公司一些软件都是WPF布局,加上工作内容涉及Socket通讯较多,决定用WPF做一个通讯小 ...
- MyBatis学习(五)MyBatis-开启log4j日志
1.前言 Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护进程等 ...
- nim_duilib(7)之TreeView
introduction 更多控件用法,请参考 here 和 源码. 本文的代码基于这里 xml文件添加代码 基于上一篇, 继续向basic.xml中添加下面关于TreeView的代码. xml完整源 ...
- ubuntu下载源码clang + llvm+lldb 编译+安装
[本文可能涉及到Ubuntu安装以下工具:] A.g++ B.gcc C.make D.cmake E.clang(10.0.1)(必须) F.llvm(10.0.1)(必须) G.lldb(10.0 ...
- 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Web前端面试题整合,持续更新【可以收藏】
饭后闲来无事,把这几年带学生用的一些面试题整合一下,供上!拿走,不客气!应付一般公司的二面基本上是够用了.祝你早日拿到心仪的offer. css相关 1. 万能居中 1.margin: 0 auto; ...
- Python调用Prometheus监控数据并计算
Prometheus是什么 Prometheus是一套开源监控系统和告警为一体,由go语言(golang)开发,是监控+报警+时间序列数 据库的组合.适合监控docker容器.因为kubernetes ...