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.

分析:

这里类似于在array上面放一个window, window的左边从第一个数开始,增加window的size, 保证window里的数之和大于S,然后每当window 右边往前走一步(window变大),就check是否可以把window左边cover的值remove掉(window 变小),并同时update minLength.

public class Solution {
public int minSubArrayLen(int s, int[] nums) {
if (nums == null || nums.length == ) return ;
int start = , total = ;
int minLength = Integer.MAX_VALUE;
for (int end = ; end < nums.length; end++) {
total += nums[end];
if (total >= s) {
minLength = Math.min(minLength, end - start + );
}
while (start <= end && total - nums[start] >= s ) {
total -= nums[start];
start++;
minLength = Math.min(minLength, end - start + );
}
} if (total < s) return ;
return minLength;
}
}

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. 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. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

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

  5. 【刷题-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 ...

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

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

  7. Minimum Size Subarray Sum LT209

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

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

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

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

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

  10. 【leetcode】Minimum Size Subarray Sum(middle)

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

随机推荐

  1. 用Random类输出验证码

    package dx; import java.text.DecimalFormat; import java.util.Random; public class DxL { //创建类 public ...

  2. 【BZOJ 3674】可持久化并查集加强版&【BZOJ 3673】可持久化并查集 by zky 用可持久化线段树破之

    最后还是去掉异或顺手A了3673,,, 并查集其实就是fa数组,我们只需要维护这个fa数组,用可持久化线段树就行啦 1:判断是否属于同一集合,我加了路径压缩. 2:直接把跟的值指向root[k]的值破 ...

  3. codevs 1378选课 树形DP

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ],tr[] ...

  4. 复习CSS

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Java基础-String 存储机制管理

    JVM运行的时候,将内存分为两个部分,一部分是堆,一部分是栈.堆中存放的是创建对象,而栈中存放的则是方法调用过程中的局部变量或引用.在设计JAVA字符串对象内存实现的时候,在堆中又开辟了一块很小的内存 ...

  6. 【Matplotlib】 移动spines

    相关文档: Spines Axis container Transformations tutorial Spines 是连接轴刻度标记的线,而且标明了数据区域的边界. 他们可以被放置在任意位置.直到 ...

  7. 【poj1080】 Human Gene Functions

    http://poj.org/problem?id=1080 (题目链接) 题意 给出两个只包含字母ACGT的字符串s1.s2,可以在两个字符串中插入字符“-”,使得s1与s2的相似度最大. Solu ...

  8. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  9. HDU2509 Be the Winner

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. ACdream 1429 Rectangular Polygon

    Rectangular Polygon Time Limit: 1000MS   Memory Limit: 256000KB   64bit IO Format: %lld & %llu D ...