Minimum Subarray

原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/#

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

Return the sum of the subarray.

注意

The subarray should contain at least one integer.

样例

For [1, -1, -2, 1], return -3

标签 Expand

SOLUTION 1:

1. 我们把整个array * -1,把它转化为求最大和的题目。

2. 然后我们可以使用Sliding window的方法来做。记录一个sum,如果发现sum<0 则丢弃,否则不断把当前的值累加到sum,

3. 每计算一次sum,求一次max.

4. 返回-max即可。

 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 len = nums.size(); int max = Integer.MIN_VALUE;
int sum = 0;
for (int i = 0; i < len; i++) {
if (sum < 0) {
sum = -nums.get(i);
} else {
sum += -nums.get(i);
} max = Math.max(max, sum);
} return -max;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/array/SubarraySum.java

Lintcode: Minimum Subarray 解题报告的更多相关文章

  1. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  2. Lintcode: Minimum Subarray

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

  3. Maximum Subarray解题报告zz

    http://fisherlei.blogspot.com/2012/12/leetcode-maximum-subarray.html Find the contiguous subarray wi ...

  4. 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...

  5. 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...

  6. 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...

  7. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  8. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...

  9. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

随机推荐

  1. Android开发环境——模拟器AVD相关内容汇总

     Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT相关 ...

  2. Windows ElasticSearch中文分词配置

    elasticsearch官方只提供smartcn这个中文分词插件,效果不是很好,好在国内有medcl大神(国内最早研究es的人之一)写的两个中文分词插件,一个是ik的,一个是mmseg的,下面分别介 ...

  3. Postman 网络调试工具

    1.Postman 简介 Postman 是一款功能强大的网页调试与发送网页 HTTP 请求的工具.我们可以用来很方便的模拟 get 或者 post 或者其他方式的请求来调试接口. 官网下载地址 Po ...

  4. xtrabackup 源码安装

    安装依赖包:这些依赖包必须要先安装好 # yum install cmake libaio-devel ncurses-devel bzip2-devel libxml2-devel libgcryp ...

  5. SLA等级那些9的实际意义

    1. 重要的系统起码要设计达到99.9%的可靠性吧. 俗称3个9,这是什么意思呢? (1-99.9%)*365*24=8.76小时,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是8.76小 ...

  6. 设计模式之策略模式&amp;简单工厂模式

    学习设计模式已经有非常长一段时间了,事实上先前已经敲过一遍了.可是老认为没有学到什么,认识也不够深刻.如今趁着重构机房,再又一次来过,也不晚. 事实上在敲了机房之后,看看模式,事实上,曾经非常难理解. ...

  7. Spring boot validation校验

    使用 Hibernate validator 的步骤:1. 在 Pojo 类的字段上, 加上 Hibernate validator 注解2. 在Controller 函数的形参前加上 @Valid ...

  8. nginx 443 https mark

    #user  nobody; worker_processes  4; #error_log  logs/error.log; #error_log  logs/error.log  notice; ...

  9. iphone 4s插件的安装,问题及美化

    此处iphone4s为美版,系统版本为5.0.1 首先添加我自己的weiphone源:http://apt.weiphone.com/u/2903862以及破解资源源:http://cydia.xse ...

  10. hbase操作

    名称命令表达式 创建表create '表名称','列簇名称1','列簇名称2'....... 添加记录put '表名称', '行名称','列簇名称:','值' 查看记录get '表名称','行名称' ...