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. 手把手带你撸深度学习经典模型(一)----- UNet
  2. Django 3.2.5博客开发教程:HelloWorld欢迎页面
  3. boost::outcome模块outcome相关的测试程序
  4. Android之URI简介
  5. java string rt_如何使jvm加载我的java.lang.String而不是rt.jar中的那个
  6. 流程图虚线框表示什么_UI设计|APP的交互线框布局设计
  7. 【蓝桥杯单片机】矩阵键盘和独立键盘新解(更稳定更高复用性)
  8. Pycharm汉化包+操作步骤
  9. 那些年Android黑科技②:欺骗的艺术
  10. Java自动化测试——打开浏览器
  11. java 拼音搜索功能设计与实现
  12. 分布式机器学习第3章 分布式机器学习框架
  13. MATLAB强化学习实战(三) 使用并行计算训练DQN智能体进行车道保持辅助(LKA)
  14. GCC警告warning: control reaches end of non-void function [-Wreturn-type]|
  15. 使用 AES 对称加密算法对视频文件进行加密解密(C++ 及 Java 实现)
  16. http缓慢攻击漏洞
  17. Invalid bound statement (not found): com.itheima.mapper.userMapper.SelectAll
  18. 硬盘为raw格式后的修复
  19. VS2010通过ADO连接EXCEL数据库
  20. ROS的故事(张新宇版,有适当删减)

热门文章

  1. gltexsubimage2d 性能_风水轮流转!AMD R5 5600X单核性能碾压十代i9
  2. 页式存储管理程序模拟_ADAS/AD开发12 - 数据存储管理
  3. oracle触发器不允许修改数据库,Oracle数据库使用触发器记录表数据修改记录
  4. api php usdt 以太坊_以太坊智能地址PHP开发包【零GAS归集ERC20】
  5. libgdx使用android控件,在Android上使用libGDX中的SQLite
  6. python多态_多态是什么?为什么要使用多态?
  7. docker启动后自动退出_Spring Boot项目启动后如何自动执行逻辑
  8. java 远程查看电脑磁盘,请问你如何在Java中监视计算机的CPU、内存和磁盘使用情况?...
  9. linux执行指定程序的命令,linux 下使用指定的用户来执行命令
  10. java main启动spring_gradle 搭建springMVC项目,main函数启动