题目:

给定一个整数数组,找到一个具有最小和的子数组。返回其最小和。

样例

给出数组[1, -1, -2, 1],返回 -3

注意

子数组最少包含一个数字

解题:

和最大子数组 ,差不多的,动态规划的还是可以继续用的

Java程序:

public class Solution {
/**
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(ArrayList<Integer> nums) {
// write your code
int min_ending_here = nums.get(0);
int min_so_far = nums.get(0);
for (int i=1 ;i< nums.size(); i++){
min_ending_here = Math.min(nums.get(i),nums.get(i) + min_ending_here);
min_so_far = Math.min( min_so_far, min_ending_here );
}
return min_so_far;
}
}

总耗时: 2153 ms

Python程序:

class Solution:
"""
@param nums: a list of integers
@return: A integer denote the sum of minimum subarray
"""
def minSubArray(self, nums):
# write your code here
min_ending_here = min_so_far = nums[0]
for x in nums[1:]:
min_ending_here = min(x, min_ending_here + x)
min_so_far = min(min_so_far , min_ending_here)
return min_so_far

总耗时: 648 ms

上面的空间复杂度是O(1),定义一个数组dp[i] ,表示当前i位置时候的最小子数组的值,最后再变量找出dp的最小值就是答案。

public class Solution {
/**
* @param nums: a list of integers
* @return: A integer indicate the sum of minimum subarray
*/
public int minSubArray(ArrayList<Integer> nums) {
// write your code
if( nums == null)
return 0;
int n = nums.size();
int dp[] = new int[n];
dp[0] = nums.get(0);
for(int i=1;i<n;i++){
int tmp = dp[i-1] + nums.get(i);
if(tmp > nums.get(i))
dp[i] = nums.get(i);
else
dp[i] = tmp;
}
int Min = Integer.MAX_VALUE;
for(int i =0;i< n;i++){
if(dp[i] < Min)
Min = dp[i];
}
return Min;
}
}

Java Code

总耗时: 2556 ms

lintcode:Minimum Subarray 最小子数组的更多相关文章

  1. Lintcode: Minimum Subarray 解题报告

    Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...

  2. Lintcode: Minimum Subarray

    Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Hav ...

  3. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  4. lintcode.44 最小子数组

    最小子数组   描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题 ...

  5. lintcode 中等题:和大于S的最小子数组

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

  6. Windows10系统如何安装Microsoft Visual Studio 2015及最小子数组和求解

    一.Windows10系统如何安装Microsoft Visual Studio 2015. 1.首先到Visual Studio官方网站(https://www.visualstudio.com/v ...

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

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

  8. 和大于S的最小子数组 · Minimum Size Subarray Sum

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

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

随机推荐

  1. JQuery window.opener

    $('#Save').click(function () {    var parent = $(parent.document.body);    $(parent).find('input#add ...

  2. 本地安装gem install --local redis-stat-0.4.13.gem

    因为主机环境不能联外网,悲哀,所以只能想办法下载包,上传到主机来安装 环境:el6.x86_64 1. gem 安装[http://centos.ustc.edu.cn/centos/6/os/x86 ...

  3. [转]AIX下调整分区大小

    AIX下调整文件系统大小 - [work] 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://wangsuiri.blogbus.com/logs/35448074.htm ...

  4. php获取服务器时间的代码

    php获取服务器时间的代码. 用php的date函数即可来获取服务器上的时间:  <?php //将时区设置为中国 date_default_timezone_set("PRC&quo ...

  5. Eclipse导入android包错误

    错误提示:Invalid project description… 解决方案:假设你的工作空间是workshop,那么你可以在你的workshop下新建一个文件夹,然后放入你的包,再在Eclipse中 ...

  6. Linux&UNIX上卸载GoldenGate的方法

    1. Log on to the database server (as oracle) where the GoldenGate software is installed. [root@oracl ...

  7. 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?

    package common; public class ShortTypeTest { /* * @param args */ public static void main(String[] ar ...

  8. ubuntu 12.04版本出现界面终端打开broken pipe,但是tty1这些可以。

    sudo apt-get remove xserver-xorg sudo apt-get install xserver-xorg

  9. IOS开发之──应用之间调用(2)

    在上一篇文章中,讲解了如何在自己应用之间调用问题,今天介绍一下如果调用IOS自带的app的方法 一.调用app store界面方法 在实际开发中,往往要推荐自己其他应用和推荐自己的收费软件,那么我们就 ...

  10. cmd下windows批处理,获取当前系统时间,生成日志文件名

    示例: rdGetRTData_log%date:~0,4%%date:~5,2%%date:~8,2%.txt 生成格式: rdGetRTData_log20151103.txt 编写Windows ...