1. 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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode 209 Minimum Size Subarray Sum

    Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  4. 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 ...

  5. 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 ...

  6. 【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 ...

  7. 209. Minimum Size Subarray Sum(双指针)

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  8. [刷题] 209 Minimum Size Subarray Sum

    要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3 ...

  9. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

随机推荐

  1. LuoguB2013 温度表达转化 题解

    Content 输入华氏温度 \(F\),请将其转化为摄氏温度 \(C\),精确到小数点后 \(5\) 位. 数据范围:\(F\geqslant -459.67\). Solution 简单的输入输出 ...

  2. React-Router(一)

    React-Router基础知识 import React from "react"; import { BrowserRouter as Router, Switch, Rout ...

  3. JAVA发送xml格式的接口请求

    /** * * @param urlStr 接口地址 * @param xmlInfo xml格式参数数据 * @return */ public static String sendMsgXml(S ...

  4. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

  5. 【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/domino-a ...

  6. leetcode 761. Special Binary String

    761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...

  7. 【剑指Offer】字符流中第一个不重复的字符 解题报告(Python)

    [剑指Offer]字符流中第一个不重复的字符 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interv ...

  8. 【MySQL作业】DDL 和 DML——美和易思使用 DML 新增和更新表数据应用习题

    点击打开所使用到的数据库>>> 1.添加 easyShopping 客户数据. insert into customer values('abc111','111',' 刘一鸣 ', ...

  9. AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at

    AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at C:\Users\Adminis ...

  10. MobaXterm远程连接Linux图形用户界面

    目标: 在自己的Windows桌面打开运行在Linux上的firefox浏览器, 使用MobaXterm终端工具在命令行直接打开图像化界面. 工具: Windows: MobaXterm Linux: ...