Minimum Size Subarray Sum LT209
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.
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的更多相关文章
- [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 ...
- 领扣-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 ...
- 和大于S的最小子数组 · Minimum Size Subarray Sum
[抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...
- [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 ...
- 【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 ...
随机推荐
- 2.6、CDH 搭建Hadoop在安装(安装CDH和其他软件)
第6步:安装CDH和其他软件 设置Cloudera Manager数据库后,启动Cloudera Manager Server,然后登录Cloudera Manager Admin Console: ...
- excel表格输入思想
1.创建工作簿 SXSSFWorkbook wb = new SXSSFWorkbook(); //#设置单元格的垂直居中,水平居中,字体颜色 2.创建sheet Sheet sheet = wb ...
- Oracle11g RAC安装
双节点RAC环境,数据库 racdb 实例1:racdb1 实例2:racdb2 1.IP规划 名称 oracle-db1 oracle-db2PUBLIC I ...
- data属性(The Data Attribute)
HTML片段 <div id="myDiv" data-custom-attr="My Value"> 巴拉巴拉,lady 嘎嘎 </div& ...
- js常用返回网页顶部几种方法
一.使用锚标记 此方法最简单,只需在body下放个隐藏的锚点标记,内容如下: 代码如下 复制代码 <a name="top" id="top">& ...
- with as 如何工作
with as 如何工作 with如何工作? Python对with的处理还是很机智滴.基本思想就是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法 紧跟wi ...
- leetcode 树类型题
树的测试框架: // leetcodeTree.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- 第十一章 串 (a)ADT
- Ant 使用指南 与 知识汇总
一.Ant是什么? Ant是一种基于Java和XML的build工具.它可以帮助我们将项目开发过程中需要完成的各种步骤组织起来,通过一个简易的方式来构建整个项目.Ant究竟能做什么呢?这 ...
- NYOJ_矩形嵌套(DAG上的最长路 + 经典dp)
本题大意:给定多个矩形的长和宽,让你判断最多能有几个矩形可以嵌套在一起,嵌套的条件为长和宽分别都小于另一个矩形的长和宽. 本题思路:其实这道题和之前做过的一道模版题数字三角形很相似,大体思路都一致,这 ...