Lintcode: Minimum Subarray
Given an array of integers, find the subarray with smallest sum. Return the sum of the subarray. Have you met this question in a real interview? Yes
Example
For [1, -1, -2, 1], return -3 Note
The subarray should contain at least one integer.
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 local = nums.get(0);
int global = nums.get(0);
for (int i=1; i<nums.size(); i++) {
local = Math.min(local+nums.get(i), nums.get(i));
global = Math.min(local, global);
}
return global;
}
}
Lintcode: Minimum Subarray的更多相关文章
- Lintcode: Minimum Subarray 解题报告
Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...
- lintcode:Minimum Subarray 最小子数组
题目: 最小子数组 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 样例 给出数组[1, -1, -2, 1],返回 -3 注意 子数组最少包含一个数字 解题: 和最大子数组 ,差不多的 ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LintCode笔记了解一下]44.Minimum Subarray
这道题和max subarray很类似,我用local 和 global 的dp方式阔以解决这道 那么我们来看动态规划的四个要素分别是什么? State: localmin[i] 表示以当前第i个数最 ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- LintCode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Lintcode: Minimum Adjustment Cost
Given an integer array, adjust each integers so that the difference of every adjcent integers are no ...
随机推荐
- laravle faker
1.编辑 /database/factories/ModelFactory,添加新的类模型填充 $factory->define(App\Post::class, function (Faker ...
- JS基础语法
1.注释语法://单行注释./*多行注释*/. 2.输出语法:{1.alert("要输出的字符串"):.alert(输出其类型): 2.confirm():弹出一个可以和用户交互 ...
- Delphi下的OpenGL开发入门
unit Unit1; interface uses OpenGL,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls ...
- bpel 之伙伴
一.伙伴链接类型(Partner Link Types) 1.交互过程 伙伴之间的交互过程共分为两种典型情况: 流程调用伙伴后同步等待返回结果.这种情况通常是伙伴能很快返回结果,流程不需要等待很长时间 ...
- dom paser
dom paser /** * */ package ec.utils; import java.io.BufferedInputStream; import java.io.ByteArrayInp ...
- getComputedStyle()与currentStyle
getComputedStyle()与currentStyle计算元素样式 发表于 2011-10-27 由 admin “DOM2级样式”增强了document.defaultView,提供了get ...
- 通过全局设置过滤器,就能让所有窗口都可移动,而不是都要继承指定QDialog
#include "appinit.h" #include <QMouseEvent> #include <QApplication> #include & ...
- php--linux环境下的主从复制
1.编辑数据库配置文件my.cnf,一般在/etc/目录下. #vi /etc/my.cnf 在[mysqld]的下面加入下面代码:[第一步查看本文件夹中代码是否已经存在,存在不需要进行添加] 只是修 ...
- 【android学习2】:Eclipse中HttpServlet类找不到
Eclipse中使用的HttpServlet类之所以识别不到的原因是没有导入Servlet-api.jar包,这个包在所安装在的tomcat的lib文件下,所以只需要导入即可. 在需要导入的工程上右键 ...
- [RVM is not a function] Interating RVM with gnome-terminal
Ubuntu 12.04 64bit LTS, running the 'rvm use 1.9.3' brings the 'RVM is not a function' warning. Here ...