Maximum Subarray 解答
Question
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
Solution 1 -- DP
New array dp[i] has the following meaning:
dp[i] is the largest sum of subsequence (including nums[i]).
public class Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length < 1)
return 0;
int length = nums.length, result = nums[0];
int[] dp = new int[length];
dp[0] = nums[0];
for (int i = 1; i < length; i++) {
dp[i] = Math.max(nums[i], nums[i] + dp[i - 1]);
result = Math.max(result, dp[i]);
}
return result;
}
}
Solution 2
We can only use one variable instead of array.
public class Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length < 1)
return 0;
int length = nums.length, result = nums[0], tmp = nums[0];
for (int i = 1; i < length; i++) {
tmp = Math.max(nums[i], nums[i] + tmp);
result = Math.max(result, tmp);
}
return result;
}
}
Maximum Subarray 解答的更多相关文章
- 刷题53. Maximum Subarray
一.题目说明 题目是53. Maximum Subarray,求最长连续子序列最大和.难度是Easy! 二.我的解答 Easy的题目,居然没做出来. 后来看了用dp方法,其中dp[i]表示以第i个元素 ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
随机推荐
- Source insight添加工具自动排版
当在网上找了一些别人的程序拿来学习,用Source insight来看时,会不会因为代码太乱看了义愤填膺呢? 有很多集成的开发环境可以自动排版,但source insight却不行!不过,有工具和配置 ...
- Java异步调用Future对象
Future类存在于JDK的concurrent包中,主要用途是接收Java的异步线程计算返回的结果. 个人理解的使用场景大概如下: 有两个任务A和B,A任务中仅仅需要使用B任务计算成果,有两种方法实 ...
- python高级编程之最佳实践,描述符与属性01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #最佳实践 """ 为了避免前面所有的 ...
- Linux正則表達式-反复出现的字符
星号(*)元字符表示它前面的正則表達式能够出现零次或多次.也就是说,假设它改动了单个字符.那么该字符能够在那里也能够不在那里,而且假设它在那里,那可能会不止出现一个.能够使用星号元字符匹配出如今引號中 ...
- C++空类中的默认函数
定义一个空的C++类,例如 class Empty { } 一个空的class在C++编译器处理过后就不再为空,编译器会自动地为我们声明一些member function,一般编译过去就相当于 cla ...
- jsp当参数为空的时候默认显示值
当${business.branchName }为空或者不存在的时候显示“请选择门店” <c:out value="${business.branchName }" defa ...
- My way to Python - Day03
列表和字典的赋值 dict1 = {} dict1['k1'] = 'v1' list1 = [] list1.append('v1') 集合系列 1,计数器 Python 2.7.6 (defaul ...
- winsocket <研究了一天的成果>
首先,这都是套路( ▼-▼ ) 头文件 #include <WinSock.h> #include <Winsock2.h> 用这个,这个是升级版 各个函数解释 1.W ...
- (转) Dynamic memory
In the programs seen in previous chapters, all memory needs were determined before program executi ...
- css布局学习笔记之box-sizing
当你设置了元素的宽度,实际展现的元素却能够超出你的设置:因为元素的边框和内边距会撑开元素. .div{ width: 500px; margin: 20px auto; padding: 50px; ...