LintCode 406: Minimum Size
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的更多相关文章
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 和大于S的最小子数组 · Minimum Size Subarray Sum
[抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...
- 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 【刷题-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 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 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. ...
- lintcode:Minimum Subarray 最小子数组
题目: 最小子数组 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 样例 给出数组[1, -1, -2, 1],返回 -3 注意 子数组最少包含一个数字 解题: 和最大子数组 ,差不多的 ...
随机推荐
- 6/2 sprint2 看板和燃尽图的更新
- IBM存储降级告警等一些服务器问题/dd/ethtool
1.IBM存储降级告警 一般两种情况 a.端口降级 例如模块16G->8G(IBM储存端口自适应) b.系统在作raid后,有硬盘损坏,降级 黄灯告警 2. dimm error dimm内存插 ...
- SSM整合CRUD操作(一)
http://www.cnblogs.com/loger1995/p/6352179.html?utm_source=itdadao&utm_medium=referral 说明:这是我刚开始 ...
- PAT 甲级 1063 Set Similarity
https://pintia.cn/problem-sets/994805342720868352/problems/994805409175420928 Given two sets of inte ...
- break,continue,return 的区别
(1)break 跳出当前循环体 (2)continue 跳过当前循环体continue后面的代码,继续执行下一个循环 (3)return 和循环没关系,就是跳出该函数
- Fetch POST All in One
Fetch POST All in One FPAIO "use strict"; /** * * @author xgqfrms * @license MIT * @copyri ...
- noip模拟题《序》sort
[问题背景] zhx 给他的妹子们排序.[问题描述] zhx有N个妹子,他对第i个妹子的好感度为ai, 且所有ai两两不相等.现在N个妹子随意站成一 排,他要将她们根据好感度从小到 ...
- android面试(2)----组件
1.anroid:id的作用? android:id是作为控件的唯一标示符.可以使用与releativelayout中,也可以再Activity中通过findviewbyid来获得指定的控件. 2.a ...
- java中main函数怎么调用外部非static方法
使用外部方法时(不管是static还是非static),都要先new一个对象,才能使用该对象的方法. 举例如下: 测试函数(这是错误的): public class Test { public sta ...
- [AT2064] [agc005_f] Many Easy Problems
题目链接 AtCoder:https://agc005.contest.atcoder.jp/tasks/agc005_f 洛谷:https://www.luogu.org/problemnew/sh ...