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 ...
随机推荐
- SWFObject2.0
PROBLEM: using % in the height and width(e.g. 100%) with the SWFObject 2.0 (or 2.1) with dynamic pub ...
- 在HTML中怎么去掉a标签(超链接)的下划线?
<style type="text/css">a:link,a:visited{ text-decoration:none; /*超链接无下划线*/}a:hover{ ...
- JDBC对sql server的操作
1.过程: 1>注册驱动器类:Class.forName() 2>连接数据库: String url = "jdbc:sqlserver:// ...
- 学习笔记_Java_day13_三层的HelloWorld程序(15)--不错,整体三层架构学习
分三层写:养成一个架构的习惯,如何编写一个大型网站 DAO数据层 service业务层 servlet web表述层
- 误删除了Oracle的dbf文件后的解决方法
问题描述: 误删除Oracle数据库的dbf文件,在启动和关闭数据库是会提示错误. startup启动数据库时提示: ORA-01157:无法标识/锁定数据文件 ORA-01110:数据文件:‘... ...
- iOS-开发记录-UIView属性
UIView属性 1.alpha 设置视图的透明度.默认为1. // 完全透明 view.alpha = ; // 不透明 view.alpha = ; 2.clipsToBounds // 默认是N ...
- P1396 营救
P1396 营救 218 通过 571 提交 题目提供者yeszy 标签 二分 图论 并查集 福建省历届夏令营 难度 普及- 题目描述 "咚咚咚--""查水表!" ...
- OpenJudge 1806:词典find()与end()
1806:词典 总时间限制: 3000ms 内存限制: 65536kB 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入 首先输入一 ...
- python 自动化之路 day 08 面向对象进阶
面向对象高级语法部分 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 面向对象高级语法部分 经典类vs新式类 把下面代码用python2 和python3都执行一下 1 2 ...
- 二进制方式快速安装MySQL数据库命令集合
二进制方式快速安装MySQL数据库命令集合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1.安装mysql ls mysql ...