题目:

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

样例

给出数组[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. 5种IO模型

    Unix下可用的5种I/O模型分别是: 阻塞IO 非阻塞IO IO复用(select和poll) 信号驱动式IO(SIGIO) 异步IO(POSIX的aio系列函数)   阻塞式I/O模型:      ...

  2. Delphi XE5教程7:单元引用和uses 子句

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  3. Python开发【第一篇】Python基础之正则表达式补充

    正则表达式 一简介:就其本质而言,正则表达式(或RE)是一种小型的.高度专业化的标称语言,(在Python中)它内嵌在Python中,并通过re模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...

  4. 关于python的环境变量问题

    我的ubuntu安装python后,查看所有的环境变量,发现没有PYTHONPATH?对我使用python没太大影响,自己写的模块的路径问题有很多方法解决.但是现在我想将我写的模块放在一个包里,要用到 ...

  5. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  6. C# 反射学习总结

    C#中的反射可以使得程序集和类型(类.结构.委托.接口和枚举)以及类型中的成员(方法.字段.属性.事件.参数.构造函数等)都成为变量在编程中动态调用.

  7. Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AS" in the equal to operation.

    ErrorMessage Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" ...

  8. 深入理解用mysql_fetch_row()以数组的形式返回查询结果

    同mysql_result()一样,mysql_fetch_row()也可以用来获取查询结果集,其区别在于函数的返回值不是一个字符串,而是一个数组.函数定义如下. 复制代码 代码如下: array m ...

  9. (转)使用 /proc 文件系统来访问 Linux 内核的内容

    转载网址:http://www.ibm.com/developerworks/cn/linux/l-proc.html 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口/proc 文件系统是 ...

  10. android中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...