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)的复杂度 ...
随机推荐
- WPF Image触摸移动方法
1: TouchPoint mPoint = null; 2: double mOffsetX;//水平滚动条当前位置 3: double mOffsetY;//垂直滚动条当前位置 4: bool m ...
- Quartus DSE 初步应用
介绍 Design Space Explorer (DSE) is a program that automates the process of finding the optimal collec ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
- hdu 1011 Starship Troopers_树状dp
题目链接 题意:给你一棵树(必须从根节点出发),每个节点上都有bug和value,你有m个骑士,每个骑士能消灭20个bug,你必须消灭该节点的全部bug才能拿到该节点的value,问最多能拿到valu ...
- 恢复Linux下被误删除的文件(笔记)
恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4 查找:extundelete 分一个区:sda4 [root ...
- qt model/view 架构基础介绍之QListWidget
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from Py ...
- UILabel+Create
#import <UIKit/UIKit.h> @interface UILabel (Create) /** * 创建普通Label * * @param frame frame * @ ...
- python之路-随笔 python处理excel文件
小罗问我怎么从excel中读取数据,然后我百了一番,做下记录 以下代码来源于:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html ...
- android recover 系统代码分析 -- 选择进入
最近做Recovery的规范及操作指导文档,花了一些时间将流程搞清. Android利用Recovery模式,进行恢复出厂设置,OTA升级,patch升级及firmware升级.而在进入Recover ...
- C++ - 容器(container)的erase()函数
容器(container)的erase()函数 本文地址: http://blog.csdn.net/caroline_wendy/article/details/23996013 容器(contai ...