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的更多相关文章

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

  2. 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 ...

  3. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  4. 【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 ...

  5. 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 ...

  6. 【Leetcode】209. Minimum Size Subarray Sum

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

  7. [LeetCode] Minimum Size Subarray Sum 解题思路

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

  8. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

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

  9. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

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

随机推荐

  1. Bootstrap--组件之按钮组

    什么是按钮组呢?简单解释:就是把一堆的按钮放在一行或者一列中.下面来看一个实例. 按钮组嘛,首先是按钮,所以会用到.btn这个类,还有就是bootstrap提供的按钮组.btn-group这个类,所以 ...

  2. Power Map 更新日志

    2015-05-18,五月更新 Custom Regions feature,允许用户自定义区域要素,支持kml和shape格式 New customization features,包括图例/文本框 ...

  3. Microsoft_Sql_Server_2008:无法对数据库执行删除,因为它正用于复制

    解决办法: sp_removedbreplication'StationErp' DROP DATABASE StationErp

  4. html结构,第一节

    第1步: 新建一个txt文件,重命名为index.html; 第2步: 打开方式:记事本,输入下面代码: <html> <head> <title>我的第一个网页& ...

  5. 九度OJ 1201 二叉排序树

    题目地址:http://ac.jobdu.com/problem.php?pid=1201 题目描述: 输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历. 输入: 输入第一行包括一个整数n( ...

  6. C# Linq To DataTable 分组统计 DEMO

    DataTable dt = SQLLayer.Get工作量统计(beginDate, endDate);             var querySum = from t in dt.AsEnum ...

  7. jsonp跨域访问详解

    jsonp是"用来跨域的" 同源策略 首先基于安全的原因,浏览器是存在同源策略这个机制的,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. 1.随便建两个网 ...

  8. php文件锁解决少量并发问题

    阻塞(等待)模式: <?php $fp = fopen("lock.txt", "r"); if(flock($fp,LOCK_EX)) { //..处理 ...

  9. NOIP2015 普及组(Junior) 解题报告

    1. 金币 (coin.cpp/c/pas) 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收到三枚金币 ...

  10. 一个matlab数字图像处理程序的解释

    clc; %clc是清除command window里的内容 clear all; %clear是清除workspace里的变量 close all; %close all来关闭所有已经打开的图像窗口 ...