[Lintcode]Minimum Size Subarray Sum 和大于S的最小子数组
来源:互联网 发布:看股票行情的软件 编辑:程序博客网 时间:2024/05/18 01:53
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.
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.
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) { int left = 0, right = 0; int res = Integer.MAX_VALUE, min = 0; int sum = 0; while(right < nums.length) { sum += nums[right]; min += 1; if(sum >= s) { while(left <= right && sum >= s) { sum -= nums[left]; left ++; min --; } res = Math.min(min + 1, res); } else if(sum < 0) { sum = 0; left = right + 1; if(left >= nums.length) break; min = 0; } else { // } right ++; } return res == Integer.MAX_VALUE ? -1 : res; }}
0 0
- [Lintcode]Minimum Size Subarray Sum 和大于S的最小子数组
- 406.Minimum Size Subarray Sum-和大于S的最小子数组(中等题)
- leetcode209-Minimum Size Subarray Sum(最小长度子数组和)
- LintCode-和大于S的最小子数组
- lintcode-406-和大于S的最小子数组
- LintCode: 和大于S的最小子数组
- LintCode-和大于S的最小子数组
- lintcode(406)和大于S的最小子数组
- LintCode:M-和大于S的最小子数组
- 和大于S的最小子数组-LintCode
- lintcode--和大于s的最小数组
- Minimum Size Subarray Sum 和满足要求的最小窗口
- LeetCode 209. Minimum Size Subarray Sum(最小子数组之和)
- 和大于S的最小子数组
- Minimum Size Subarray Sum 数组
- lintcode刷题——和大于S的最小子数组
- [LintCode 406] Minimum Size Subarray Sum(Python)
- Minimum Size Subarray Sum-最小长度字数组和问题
- hibernate中多对一映射更新
- shell中date的一些用法
- C#创建COM组件,QT调用COM组件简单测试(VS2008+QT4.6.4)
- jsp自定义标签的问题Unable to load tag handler class
- MySQL5.7MHA+MaxScale2.0构建高可用环境
- [Lintcode]Minimum Size Subarray Sum 和大于S的最小子数组
- elasticsearch基础及java编程
- 【jzoj4908】【NOIP2016提高组】【愤怒的小鸟】【状态压缩动态规划】
- java -- 如何记住"=="与equals的区别?
- mac使用Sublime + PlantUML高效地画图
- 2016年兰州交通大学校园选拔赛
- maven设定项目编码
- [入门-7] 基础类型之对象概述
- 安卓中响应式与交互式