Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.

Example: 

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n). 
 
Idea 1. Sliding window, since numbers are positive, it means the prefix sum of subarrays is monotonic increasing. We can use either left or right side of window as the base to slide the window.
 
Time complexity: O(n)
Space complexity: O(1)
 
Take the left side as base, we need to find the smallest right index such that the sum[left..right] <= s, if the sum is smaller than s, we keep expanding right until the sum <= s, if such right index is found, the window size right - left + 1 is the minimum length of subarray starting at index left.
 
Note 1. when the termindation for find the right index has two conditions: right < nums.length && sum + nums[right] < s, the for loop terminates could mean just reaching the end of the array, might not mean the subarray sum >= s, hence we need to add extra check.
 
Note 2: The toal sum of the whole array could be less than k, in this case, such subarray doesn't exist.
 
 class Solution {
public int minSubArrayLen(int s, int[] nums) { int sum = 0;
int minLength = nums.length;
boolean flag = false;
for(int left = 0, right = 0; left < nums.length; ++left) {
for(;right < nums.length && sum + nums[right] < s; ++right) {
sum += nums[right];
} if(right < nums.length && sum + nums[right] >= s) {
flag = true;
minLength = Math.min(minLength, right - left + 1);
} sum -= nums[left];
}
if(flag) {
return minLength;
}
return 0;
}
}

Take the right as base,

 class Solution {
public int minSubArrayLen(int s, int[] nums) {
int minLength = nums.length;
boolean flag = false;
int sum = 0;
for(int left = 0, right = 0; right < nums.length; ++right) {
sum += nums[right];
while(sum >= s) {
flag = true;
minLength = Math.min(minLength, right - left + 1);
sum -= nums[left];
++left;
}
}
if(!flag) {
return 0;
}
return minLength;
}
}

Idea 2. Binary search and Cumulative sum for prefix subarray, similar to Subarray Product Less Than K LT713, for each index i, find the smallest right index such that prefix[right] - prefix[i-1] >= s.

 class Solution {
private int findIndex(int[] prefix, int left, int right, int s) {
int i = left, j = right;
while(i < j) {
int mid = i + (j - i)/2;
if(prefix[mid] - prefix[left-1] >= s) j = mid;
else i = mid + 1;
}
return i;
} public int minSubArrayLen(int s, int[] nums) {
int[] prefix = new int[nums.length + 1];
for(int i = 1; i < prefix.length; ++i) {
prefix[i] = prefix[i-1] + nums[i-1];
} boolean flag = false;
int minLength = nums.length;
for(int i = 1; i < prefix.length; ++i) {
int smallestIndex = findIndex(prefix, i, prefix.length, s);
if(smallestIndex == prefix.length) {
break;
}
else {
flag = true;
minLength = Math.min(minLength, smallestIndex - i + 1);
}
} if(!flag) {
return 0;
}
return minLength;
}
}

Minimum Size Subarray Sum LT209的更多相关文章

  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. 和大于S的最小子数组 · Minimum Size Subarray Sum

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

  8. [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 ...

  9. 【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. spring ContentNegotiationManagerFactoryBean 内容协商

    一.什么是内容协商 简单点说,就是同一资源,可以有多种表现形式,比如xml.json等,具体使用哪种表现形式,是可以协商的. 这是RESTfull的一个重要特性,Spring Web MVC也支持这个 ...

  2. SpringMVC参考

    史上最简单的 Spring MVC http://blog.csdn.net/column/details/14594.html

  3. [Android] TextView长按复制实现方法小结(转载)

    这是别人写的,既然别人总结过了,那我就不花时间研究这个了,但往后会补充一些使用经验之类的 原文地址:http://blog.csdn.net/stzy00/article/details/414778 ...

  4. java实现person类 override(重写) comparable接口

    具体题目如下: 定义一个Person类,包含姓名(name).身高(height).体重(weight),以及talk()方法,该方法的功能是,输出自己的身高和体重信息.Person类实现Compar ...

  5. 统计请求最高的TOP 5

    cat access.log |awk -F "," '{print$14}'|awk -F "\"" '{print$4}'|sort |uniq ...

  6. vue项目中,localhost可以访问,IP无法访问的问题

    用http://localhost:8082/可以访问,但是换到ip就访问不了,127.0.0.1.0.0.0.0访问也可以,就ip不行 根源----在config里面的index.js里面的modu ...

  7. pta l2-5(集合相似度)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070149828608 题意:求两个集合的相同的不同元素的 ...

  8. 微信小程序开发——点击按钮获取用户授权没反应或反应很慢的解决方法

    异常描述: 点击按钮获取用户手机号码,有的时候会出现点击无反应或很久之后才弹出用户授权获取手机号码的弹窗,这种情况下,也会出现点击穿透的问题(详见:微信小程序开发——连续快速点击按钮调用小程序api返 ...

  9. 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口

    1.可靠传输 (1)三次握手 TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接: (1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_S ...

  10. lendinghome oa 准备

    hardcode版本 估计只能过一个吧 import java.util.*; public class NextServer { Map<Integer, Integer> server ...