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

Have you met this question in a real interview?
Example

Given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the problem constraint.

Challenge

If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

LeetCode上的原题,请参见我之前的博客Minimum Size Subarray Sum。

解法一:

class Solution {
public:/*** @param nums: a vector of integers* @param s: an integer* @return: an integer representing the minimum size of subarray*/int minimumSize(vector<int> &nums, int s) {int res = INT_MAX, sum = 0, left = 0;for (int i = 0; i < nums.size(); ++i) {sum += nums[i];if (sum >= s) {while (left < i && sum >= s) {res = min(res, i - left + 1);sum -= nums[left++];}}}return res == INT_MAX ? -1 : res;}
};

解法二:

class Solution {
public:/*** @param nums: a vector of integers* @param s: an integer* @return: an integer representing the minimum size of subarray*/int minimumSize(vector<int> &nums, int s) {int res = INT_MAX, n = nums.size();vector<int> sums(n + 1, 0);for (int i = 1; i < n + 1; ++i) sums[i] = sums[i - 1] + nums[i - 1];for (int i = 0; i < n + 1; ++i) {int left = i + 1, right = n, t = sums[i] + s;while (left <= right) {int mid = left + (right - left) / 2;if (sums[mid] < t) left = mid + 1;else right = mid - 1;}if (left == n + 1) break;res = min(res, left - i);}return res == INT_MAX ? -1 : res;}
};

[LintCode] Minimum Size Subarray Sum 最小子数组和的大小相关推荐

  1. Minimum Size Subarray Sum 最短子数组之和

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

  2. 209. Minimum Size Subarray Sum 长度最小的子数组

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

  3. leetcode 209. Minimum Size Subarray Sum | 209. 长度最小的子数组(Java)

    题目 https://leetcode.com/problems/minimum-size-subarray-sum/ 题解 双指针解法,左指针和右指针在合适的时候向右走,并维护一个sum 版本1 思 ...

  4. [JAVA]寻找满足和的最短子序列(Minimum Size Subarray Sum)

    题目来源:leetcode 题目描述: Given an array of n positive integers and a positive integer s, find the minimal ...

  5. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  6. Leetcode209-Minimum Size Subarray Sum

    新的个人博客:Hu Haoyu's Blog,欢迎参观! Given an array of n positive integers and a positive integer s, find th ...

  7. LeetCode 325. Maximum Size Subarray Sum Equals k

    这一题开始以为是sliding window,后来发现,因为有负数,sum不具有单调性,没有办法用slidng window找出optimal solution. 如果考虑brute force的做法 ...

  8. LintCode Python 简单级题目 41.最大子数组 - 44.最小子数组和

    题目1 最小子数组 描述: 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, -1, - ...

  9. 领扣LintCode问题答案-44. 最小子数组

    领扣LintCode问题答案-44. 最小子数组 目录 44. 最小子数组 鸣谢 44. 最小子数组 给定一个整数数组,找到一个具有最小和的连续子数组.返回其最小和. 子数组最少包含一个数字 样例 1 ...

最新文章

  1. 无需卷积,完全基于Transformer的首个视频理解架构TimeSformer出炉
  2. Redis 敢在线上做Keys正则匹配操作!你可以离职了!
  3. 【通知】CCIE LAB北京考场9月1日起开放考试
  4. 【干货】迅雷产品经理:浅析用户成长体系
  5. 关于ABAP高级顾问应该掌握的技术
  6. I.MX6 Android Linux shell MMPF0100 i2c 获取数据
  7. 新CIO:Mark Schwartz认为的领先IT
  8. 个人笔记 vue npm redis
  9. app令牌登录解决session超时重登陆的问题
  10. 在Ubuntu下后台持续运行Python程序
  11. mate20pro换鸿蒙系统,鸿蒙2.0下载
  12. python如何实现手眼定标_kafka基础和python如何操作kafka
  13. 论手机网站(wap)网页广告防屏蔽的方法
  14. ansiblea基本使用
  15. 结对项目—地铁出行路线规划
  16. canvas应用——圆角矩形图片
  17. signature=5a537e48de3abe15561f136edabc54dc,Visual Signatures in Video Visualization
  18. 普通的单模光纤和单模光纤跳线是否可以达到万兆速度?
  19. 房屋装修(卫生间/浴室)
  20. Anroid 7.0 Direct Boot研究

热门文章

  1. 服务器 远程存储,数据储存——远程服务器存储——框架方式
  2. php任意文件删除漏洞,phpshe后台任意文件删除漏洞及getshell | CN-SEC 中文网
  3. linux卸载es,Ubuntu卸载elasticsearch
  4. linux应用系统使用率,Linux性能优化实战:系统CPU使用率高,但为啥找不到高的应用(06)...
  5. java webengine_如何以Java实现网页截图技术
  6. android q测试机型,小米9安卓Q系统刷机包开启测试 小米Android Q适配机型一览
  7. python网络通信
  8. log4j debug写法
  9. Java编程在线学习靠谱吗?能学会吗?
  10. numpy的cumsum ()函数