lintcode 中等题:和大于S的最小子数组
题目
给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组。如果无解,则返回 -1。
给定数组 [2,3,1,2,4,3]
和 s = 7
, 子数组 [4,3]
是该条件下的最小长度子数组。
如果你已经完成了O(n)时间复杂度的编程,请再试试 O(n log n)时间复杂度。
解题
定义两个指针,slow,fast,以先后速度向右走
fast先找到第一个是的sum>s的值
根据fast和slow计算当前子数组的长度
sum-=nums[slow],寻找最后一个不满足sum>=s 的slow,每次更新子数组长度的最短值。
说明:
子数组是原始数组中连续的一部分
public class Solution {
/**
* @param nums: an array of integers
* @param s: an integer
* @return: an integer representing the minimum size of subarray
*/
public int minimumSize(int[] nums, int s) {
// write your code here
if(nums ==null || nums.length <=0)
return -1;
int slow = 0;
int fast = 0;
int n = nums.length;
int sum = 0;
int minsize = n+1;
while(fast<n){
while(fast<n && sum<s){ // 找到sum>s 的下标
sum+=nums[fast];
fast++;
}
minsize = Math.min(minsize, fast - slow + 1); while(sum>=s){ // 去除左边,也满足sum<s
sum-=nums[slow];
slow++;
minsize= Math.min(minsize, fast - slow + 1); } }
minsize= minsize==n+1?-1:minsize; // 不存在时候
return minsize;
}
}
class Solution:
# @param nums: a list of integers
# @param s: an integer
# @return: an integer representing the minimum size of subarray
def minimumSize(self, nums, s):
# write your code here
if s == None or len(nums) == 0:
return -1;
lens = len(nums)
slow = 0
fast = 0
sum = 0
res = lens+1
while fast < lens:
while fast < lens and sum < s:
sum += nums[fast]
fast +=1
while slow < fast and sum>= s:
res = min(res,fast - slow)
sum -= nums[slow]
slow +=1
if res ==lens+1:
return -1
else:
return res
Python Code
总耗时: 444 ms
lintcode 中等题:和大于S的最小子数组的更多相关文章
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:permutations II 重复数据的全排列
题目 带重复元素的排列 给出一个具有重复数字的列表,找出列表所有不同的排列. 样例 给出列表 [1,2,2],不同的排列有: [ [1,2,2], [2,1,2], [2,2,1] ] 挑战 使用递归 ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- lintcode 中等题: Implement Trie
题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assu ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:A + B Problem A + B 问题
题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...
- lintcode 中等题:搜索旋转排序数组II
题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...
随机推荐
- PHP 5.3.X 连接MS SQL Server php_mssql.dll
在网上搜索了一下PHP 5.3.X 连接SQL Server的办法,有人也遇到了这个问题 原来PHP 团队在PHP 5.3 中移除了SQL Server的驱动和库,而微软自己开发了针对PHP的SQL驱 ...
- smarty安装及例子
环境: smarty3.1.16 1.在http://www.smarty.net/download下载最新smarty包,window选择zips,linux下选择tar.gz.以windows为例 ...
- Laravel 5 基础(二)- 路由、控制器和视图简介
查看 app/Http/routes.php Route::get('/', 'WelcomeController@index'); @是一个界定符,前面是控制器,后面是动作,表示当用户请求url / ...
- vim使用手册
1. 关于Vim 1.1 Vim的几种模式 2. 启动Vim 3. 文档操作 4. 光标的移动 4.1 基本移动 4.2 翻屏 4.3 标记 5. 插入文本 5.1 基本插入 5.2 改写插入 6. ...
- iOS JSON解析
解析json成dic对象 -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error; NSDi ...
- css/js online online code editor/formator/debuger
http://cssdeck.com/labs http://jsfiddle.net/ http://fiddle.jshell.net/ support console http://plnkr ...
- MVC与WebForm的一些区别
MVC与WebForm的一些区别 它们都是ASP.NET WEB开发的两种方式 .但是他们也是有一些不同.做个小结. 1.MVC是没有服务器端控件这么一说的,也就是没有viewstate,也就不会产生 ...
- bnuoj 16493 Just Pour the Water(矩阵快速幂)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...
- java 把URL中的中文转换成utf-8编码
private static final String QUERY = "餐饮"; String sr = URLEncoder.encode(QUERY); System.out ...
- spring配置事务
一.配置JDBC事务处理机制 <!-- 配置Hibernate事务处理 --> <bean id="transactionManager" class=" ...