LintCode 406: Minimum Size

题目描述

给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组。如果无解,则返回 -1。

样例

给定数组[2,3,1,2,4,3]s = 7, 子数组[4,3]是该条件下的最小长度子数组。

Thu Feb 23 2017

思路

数组的题一般都是用指针扫描的。

这里是用一前一后两个指针都从左往右移,前面的指针一直移直到和大于s为止;后面的指针此时一直右移,直到距离最短为止。

时间复杂度是\(O(n)\).

代码

// 和大于S的最小子数组
int minimumSize(vector<int> &nums, int s)
{
if (nums.size() <= 0) return -1;
int l = 0, r = 0, sum = nums[0], ans = 6e5;
while(1)
{
if (sum >= s)
{
if (r - l + 1 < ans)
{
ans = r - l + 1;
}
else
{
sum -= nums[l];
++l;
}
}
else
{
sum += nums[r+1];
++r;
}
if (r == nums.size() || l > r) break;
}
if (ans == 6e5) return -1;
return ans;
}

LintCode 406: Minimum Size的更多相关文章

  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. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  3. [LeetCode] Minimum Size Subarray Sum 解题思路

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

  4. 和大于S的最小子数组 · Minimum Size Subarray Sum

    [抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...

  5. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. 【刷题-LeetCode】209. Minimum Size Subarray Sum

    Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...

  7. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

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

  8. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  9. lintcode:Minimum Subarray 最小子数组

    题目: 最小子数组 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 样例 给出数组[1, -1, -2, 1],返回 -3 注意 子数组最少包含一个数字 解题: 和最大子数组 ,差不多的 ...

随机推荐

  1. (一)Tensorflow安装

    主要包括下面两个指令: $ sudo apt-get install python-pip python-dev $ sudo pip install --upgrade https://storag ...

  2. inside、outside和dmz之间的访问

    现有条件:100M宽带接入,分配一个合法的IP(222.134.135.98)(只有1个静态IP是否够用?);Cisco防火墙PiX515e-r-DMZ-BUN1台(具有Inside.Outside. ...

  3. [Prism框架实用分享]如何在Prism应用程序中使用日志

    前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...

  4. Mware中CentOS设置静态IP

    Mware中CentOS设置静态IP   因为之前搭建的MongoDB分片没有采用副本集,最近现网压力较大,所以准备研究一下,于是在自己电脑的虚拟机中搭建环境,但是发现之前VMware设置的是DHCP ...

  5. 【bzoj3560】DZY Loves Math V 欧拉函数

    题目描述 给定n个正整数a1,a2,…,an,求 的值(答案模10^9+7). 输入 第一行一个正整数n. 接下来n行,每行一个正整数,分别为a1,a2,…,an. 输出 仅一行答案. 样例输入 3 ...

  6. [codeforces696B]Puzzles

    B. Puzzles time limit per test  1 second memory limit per test 256 megabytes input standard input ou ...

  7. [BZOJ1503][NOI2004]郁闷的出纳员 无旋Treap

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MB Description OIER公司是一家大型专业化软件公司,有着数以万计的员 ...

  8. JAVA中的堆、栈等内存分析

    在 JAVA 中,有六个不同的地方可以存储数据 1. 寄存器( register ) 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...

  9. 【BZOJ4709】柠檬(动态规划,单调栈)

    [BZOJ4709]柠檬(动态规划,单调栈) 题面 BZOJ 题解 从左取和从右取没有区别,本质上就是要分段. 设\(f[i]\)表示前\(i\)个位置的最大值. 那么相当于我们枚举一个前面的位置\( ...

  10. 【BZOJ3437】小P的牧场(动态规划,斜率优化)

    [BZOJ3437]小P的牧场(动态规划,斜率优化) 题面 BZOJ 题解 考虑暴力\(dp\),设\(f[i]\)表示强制在\(i\)处建立控制站的并控制\([1..i]\)的最小代价. 很显然,枚 ...