题目:

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

样例

给出数组[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. php解析url的三种方法举例

    使用php解析url的三个示例. 方法一: $url="http://www.jbxue.com"; file_get_contents($url); 方法二: // CURL 方 ...

  2. Eclipse 代码提示功能设置。

    1.        解决实例化时自动补全不必要的单词问题 2.        以MyEclipse 6.5重新配图 鉴 于网上的批评之声甚大,我只想说明我的想法:这样的增强代码提示,最终是用来辅助我们 ...

  3. 第22章 项目3:万能的XML

    Mix-in:混入类,是一种Python程序设计中的技术,作用是在运行期间动态改变类的基类或类的方法,从而使得类的表现可以发生变化.可以用在一个通用类接口中. 在实践一个解析XML文件的实践中,体会动 ...

  4. 飞行器的Pitch Yaw Roll概念图解

    前进方向为Z轴 Pitch 升降 绕X轴  对应常见Φ(phi)角: Yaw 偏航 绕Y轴 对应常见θ(theta)角: Roll 翻滚 绕Z轴 对应常见φ(psi)角: 对应表: 参考网址NASA: ...

  5. 开源免费的C/C++网络库(c/c++ sockets library)

    (1)ACE 庞大.复杂,适合大型项目.开源.免费,不依赖第三方库,支持跨平台. http://www.cs.wustl.edu/~schmidt/ACE.html (2)Asio Asio基于Boo ...

  6. WPF 一个弧形手势提示动画

    这是一个操作提示动画,一个小手在屏幕上按照一个弧形来回运动 <Window x:Class="LZRichMediaWall.MainWindow" xmlns=" ...

  7. 【Web学习日记】——在IIS上发布一个WebService

    没有开发过程,只是发布过程 一.前提 开发使用的是VS2013 从来没有做过Web的发布,在网上找例子,看到的总是与自己的情况不相符,而且也有人提出了VS2013发布网站的问题,但解决方案却很少,好不 ...

  8. 系统架构师JD

    #################################################################################################### ...

  9. mysql查询差集

    select A.* from A left join B using(name,addr,age) where B.name is NULL; select A.* from A left join ...

  10. javascript实现播放音乐

    <script language="javascript"> var flag = 0; //控制变量放在函数内起不到作用.function openplay() { ...