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. CF729A Interview with Oleg 题解

    Content 给出一个长度为 \(n\) 字符串 \(s\),请将开头为 \(\texttt{ogo}\),后面带若干个 \(\texttt{go}\) 的子串替换成 \(\texttt{***}\ ...

  2. AT1445 乱数生成 题解

    Description 有一个机器会等概率从 \(1\) 到 \(n\) 的正整数中选出一个整数.显然地,这个机器运行 \(3\) 次后会得到 \(3\) 个整数.求这 \(3\) 个整数的中位数是 ...

  3. LuoguP4759 [CERC2014]Sums 题解

    Content 给定 \(t\) 组数据,每组数据给定一个数 \(n\),判断 \(n\) 是否能够分解成连续正整数和,能的话给出最小数最大的方案. 数据范围:\(1\leqslant n\leqsl ...

  4. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  5. 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)

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

  6. 1266 - Points in Rectangle

    1266 - Points in Rectangle    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  7. D. Persistent Bookcase(Codeforces Round #368 (Div. 2))

    D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  8. MySQL定时备份数据库方案

    MySQL数据备份 1.备份全部数据库的数据和结构 mysqldump -uroot -p123456 -A > /data/mysqlDump/mydb.sql 2.备份全部数据库的结构(加 ...

  9. A Simple Framework for Contrastive Learning of Visual Representations

    目录 概 主要内容 流程 projection head g constractive loss augmentation other 代码 Chen T., Kornblith S., Norouz ...

  10. Towards Deep Learning Models Resistant to Adversarial Attacks

    目录 概 主要内容 Note Madry A, Makelov A, Schmidt L, et al. Towards Deep Learning Models Resistant to Adver ...