leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum
1 题目
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 0 instead.
For 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 int minSubArrayLen(int s, int[] nums)
2 思路
题意
给定一个包含n个正整数的数组和一个正整数s,找出其满足和sum ≥ s的子数组的最小长度。如果不存在这样的子数组,返回0
例如,给定数组 [2,3,1,2,4,3]与s = 7,
子数组[4,3]具有满足题设条件的最小长度。
解题思路
O(n^2)解法:贪心法,要点:一个子数组是结果,一定有某个为起点的位置。网上说:滑动窗口法。不知道是怎么解的。
O(nlogn)解法:二分枚举,思路是,我们建立一个比原数组长一位的sums
数组,其中sums[i]
表示nums
数组中[0, i - 1]
的和,然后我们对于sums
中每一个值sums[i]
,用二分查找法找到子数组的右边界位置,使该子数组之和大于sums[i] + s
,然后我们更新最短长度的距离即可。
3 代码
/**
* Time:O(n^2) Space:O(1) 有更好的解法,时间复杂度:O(nlogn)
*/
public int minSubArrayLen(int s, int[] nums) {
int min = Integer.MAX_VALUE;
int len = nums.length;
for (int i = 0; i < len; i++) {
int count = 0, sum = 0;
for (int j = i; j < len; j++) {
sum += nums[j];
count++;
if (sum >= s) {
min = Math.min(min, count);
break;
}
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
4 总结
二分法的解法,不是很明白。手动走一遍,二分法的代码。
leetcode面试准备:Minimum Size Subarray Sum的更多相关文章
- 【刷题-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 OJ 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 【LeetCode 209】Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode OJ:Minimum Size Subarray Sum(最小子数组的和)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LintCode] 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 ...
随机推荐
- pageContext.request.contextPath
jsp:<c:set var="ctxStatic" value="${pageContext.request.contextPath}"/>嵌套d ...
- javascript 基础1第11节
<html> <head> <title>javascript基础</title> </head> <body> 1.NaN i ...
- Leetcode Count Prime
Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n ...
- Codevs 1689 建造高塔
1689 建造高塔 时间限制: 1 s 空间限制: 128000 KB 题目等级 : **钻石 Diamond** 题目描述 Description n有n种石块,石块能无限供应.每种石块都是长方体, ...
- 九度OJ 1076 N的阶乘 -- 大数运算
题目地址:http://ac.jobdu.com/problem.php?pid=1076 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: ...
- Centos学习手册——装逼宝典之强制重置密码
---恢复内容开始--- Centos学习手册by RuffianFish; 痞子鱼 近日闲的无聊,而最近又在搞Centos决定写个Centos详细的学习手册,以便自己在长时间没摸Centos的情况下 ...
- 每天一条linux命令——halt
halt命令用来关闭正在运行的Linux操作系统.halt命令会先检测系统的runlevel,若runlevel为0或6,则关闭系统,否则即调用shutdown来关闭系统. 语法: halt(选项) ...
- iOS面试题6.30总结
越来越多的人投入iOS这个行业中,但是作为刚才学校毕业的学生,我们没有任何经验.或者经验很少.但是这也不能阻挡我们对苹果的热情,想投入iOS的开发中.而作为进入企业的第一步,我们要参加面试.面试中我们 ...
- javascript进阶——面向对象特性
面向对象的javascript是这门语言被设计出来时就考虑的问题,熟悉OOP编程的概念后,学习不同的语言都会发现不同语言的实现是不同的,javascript的面向对象特性与其他具有面向对象特性的语言的 ...
- Python冒泡排序
冒泡排序,顾名思义,按照一定的规则,把数据一直排下去 直接上代码 import random def bubblesort(data): for i in range(len(data)-1,1,-1 ...