新的个人博客Hu Haoyu’s Blog,欢迎参观!

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

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

题意

给出一个包含n个正整数的数组和一个正整数s,找到长度最小的(连续)子数组使其和大于等于s。如果不存在这样的子数组,返回0。

比如数组为[2, 3, 1, 2, 4, 3],s = 7。子数组[4, 3]是长度最小的子数组,其和4+3≥7。

分析

使用一种在线处理的方法,类似“数组的最大子数组和”的O(n)解法。

步骤

  • 我们设置bottom和top控制子数组的头部和尾部。
  • 初始化bottom=0,top为-1,表示一个空的子数组
  • 子数组和sum=0,最小长度len=0。
  • 当sum < s时,在当前子数组的尾部增加一个元素bottom[++top]。
  • 当sum ≥ s时,去掉当前子数组的头部元素,并++bottom。
  • 退出循环的条件:top == nums.size() 或 bottom>top(此时已经存在最小len为1,不可能更小,可以退出)。

算法复杂度

由于top和bottom至多遍历一次数组nums,因此算法复杂度为O(n)。

更多练习

题目要求再给出一种O(nlogn)的解法。

简略分析

采用分治法的思想。每次将区间A一分为二,成为A1和A2。子问题是求两个子区间A1和A2中的各自的最小子数组长度len1和len2,以及区间A的最小子数组长度len中的最小值,即min{len1, len2, len}。

算法复杂度

主定理master定理)可知:T(n) = 2*T(n/2) + n,故算法复杂度为O(nlogn)

AC代码

O(n)及O(nlogn)算法

//O(n)
class Solution {
public:int minSubArrayLen(int s, vector<int>& nums) {if (!nums.size()) return 0;int bottom = 0, top = -1;int sum = 0, len = 0;while (true) {if (sum < s) {++top;if (top != nums.size())sum += nums[top];elsebreak;} else {sum -= nums[bottom]; ++bottom;if (bottom > top)break;}if (sum >= s) {int new_len = top - bottom + 1;if (!len || len && new_len < len)len = new_len;}}return len;}
};//O(nlogn)
class Solution {
public:int MAX_INT = 999999999;int minSubArrayLen(int s, vector<int>& nums) {if (!nums.size()) return 0;return findMinLen(s, nums, 0, nums.size() - 1);}int findMinLen(int s, vector<int>& nums, int bottom, int top) {if (top == bottom) return nums[top] >= s ? 1 : 0;int mid = (bottom + top) / 2;int left = mid, right = mid + 1, sum = 0, len;while (sum < s && (right <= top || left >= bottom)) {if  (right > top) {sum += nums[left]; --left;}else if (left < bottom) {sum += nums[right]; ++right;}else if (nums[left] > nums[right]) {sum += nums[left]; --left;}else {sum += nums[right]; ++right;}}if (sum >= s) {len = right - left - 1;int leftLen = findMinLen(s, nums, bottom, mid);int rightLen = findMinLen(s, nums, mid + 1, top);return minValue(leftLen, rightLen, len);}else {return 0;}}int minValue(int x, int y, int z) {if (!x) x = MAX_INT;if (!y) y = MAX_INT;if (x <= y && x <= z) return x;if (y <= x && y <= z) return y;return z;}
};

如分析或代码有误,请批评指正,谢谢。

Leetcode209-Minimum Size Subarray Sum相关推荐

  1. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

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

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

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

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

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

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

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

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

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

  6. LeetCode 325. Maximum Size Subarray Sum Equals k

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

  7. LintCode 402: Continuous Subarray Sum

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

  8. LeetCode 523. Continuous Subarray Sum

    题目: Given a list of non-negative numbers and a target integer k, write a function to check if the ar ...

  9. LeetCode Subarray Sum Equals K

    原题链接在这里:https://leetcode.com/problems/subarray-sum-equals-k/description/ 题目: Given an array of integ ...

最新文章

  1. Centos7安装DockerCE
  2. 2020腾讯校招后台开发
  3. ios 打电话结束返回到应用中
  4. g++编译后运行时无法链接动态库的解决方法
  5. python2/python3安装pip/pip3及使用国内镜像源(python2/3同时安装)
  6. html border阴影效果_【开发小技巧】—如何使用HTML和CSS创建浮动框效果?
  7. 教程 打造OS X Mavericks原版 EFI Clover 引导安装
  8. 蓝奏网盘直链转换器 v1.1
  9. VSCode瞎折腾记
  10. 【跃迁之路】【479天】程序员高效学习方法论探索系列(实验阶段236-2018.05.30)...
  11. linux新建mysql用户命令_使用MySQL命令行新建用户并授予权限
  12. Linux基础软件威胁疑云:从已知到“未知”
  13. mongovue使用简介
  14. 婚礼邀请函微信小程序
  15. unity -- 存档与读档
  16. 阿里数据仓库-数据模型建设方法总结(全)
  17. python输入一个小数,提取整数部分
  18. 学计算机能用到的礼物,考上大学送什么礼物好,盘点10款有趣的
  19. 高并发秒杀系统方案的优化
  20. 网站内容文章不收录什么原因?

热门文章

  1. 企业付款至零钱任务提醒助手系统开发
  2. 苹果天气不显示_今天才知道,简单几步,就能让你的iPhone手机显示锁屏天气
  3. mtb检出_Xpert MTB/RIF在儿童肺结核肺泡灌洗液检测中的应用
  4. 大厂招聘 Cocos 人才,多城市有岗
  5. 小程序支付的多场景应用
  6. 优思学院|8D和DMAIC两种方法应如何选择?
  7. 全国DNS的IP一览
  8. JS数组去重 代码实现
  9. 宏源证券数据备份之路
  10. Win10《芒果TV》更新v3.8.0初夏版:全新视觉体验,即刻分享视频