[LeetCode53]Maximum Subarray
问题:
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
思路:题意为给定 n 个整数(可能为负数)组成的序列 a[1],a[2],a[3],...,a[n],求该序列如
a[i]+a[i+1]+...+a[j]的子段和的最大值。当所给的整数均为负数时定义子段和为
0,如果序列中全部是负数则最大子断和为0,依此定义,所求的最优值为 Max{0,a[i]+a[i+1]+...+a[j]},1≤i≤j≤n。
例如 输入:-2,11,-4,13,-5,-2
输出:20
则此题可通过动态规划求解
首先计算辅助数组。
接着计算辅助数组的最大值。
辅助数组b[j]用来记录一j为尾的子段和集合中的最大子断和。
例如,假如有一序列:-2,11,-4,13,-5,-2
则
b(1) = -2 ,b(2) = 11, b(3) = 7, b(4) = 20, b(5) = 15, b(6) = 13
a(1) = -2, a(2) = 11, a(3) = 7, a(4) = 13, a(5) = -5, a(6) = -2
b(1) < 0 b(2) > 0 b(3) > 0 b(4) > 0 b(5) > 0 b(6) > 0
---->
{ b(j - 1) + a(j) 当b(j-1) >= 0
b(j) = {
{a(j) 当b(j-1) < 0
代码:
public class Solution {
public int MaxSubArray(int[] nums) {
int sum = ;
int[] dp = new int[nums.Length];
int temp = ;
for(int i = ; i < nums.Length; i++)
{
if(temp > )
temp += nums[i];
else
temp = nums[i];
dp[i] = temp;
}
sum = dp[];
for(int i = ; i < dp.Length; i++)
{
if(sum < dp[i])
sum = dp[i];
}
return sum;
}
}
[LeetCode53]Maximum Subarray的更多相关文章
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- [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)的复杂度 ...
随机推荐
- java.lang.IllegalAccessError: class javax.activation.SecuritySupport12 cannot access its superclass
最近加入新的项目组,eclipse + tomcat7 + spring +ibatis + restful 遇到了这样的问题, 说是不能访问父类,我一开始以为是版本的原因,但是久经更改,错误依然,实 ...
- POJ 2151 Check the difficulty of problems (动态规划-可能DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4522 ...
- RabbitMq消息序列化简述
涉及网络传输的应用.序列化不可避免. 发送端以某种规则将消息转成byte数组进行发送. 接收端则以约定的规则进行byte[]数组的解析. 序列化的选择能够是jdk序列化,hessian,jackson ...
- shufe前辈名师
前辈名师 姓名 现职/原职 郭秉文 中国现代大学之父.国立东南大学校长.哥伦比亚大学教育学博士,该校第一任校长.为了纪念郭秉文先生,勉励优秀学子,郭夏瑜女士在上海财经大学等校设立了“郭秉文奖学金” 马 ...
- CSharp Oracle 登陆
=======后台Oracle存储过程================ 1.创建表 --判读表存在先删除begin EXECUTE IMMEDIATE 'DROP TABLE student'; ...
- 用户手册User Guide的写法
下面的内容仅代表个人观点,是在工作中总结出来的,如果有错误之处,还请指教. 转载请注明来自博客园---”邦邦酱好“: http://www.cnblogs.com/bangbangjiang/p/36 ...
- WPF 数字小键盘Themes
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- python战斗2:看到一个页面编码
编程环境:虚拟linux (windows 下 cygwin) 识别网页编码. usage: python coding http://www.***.com 測试结果: watermark/2/te ...
- 【python下使用OpenCV实现计算机视觉读书笔记4】保存摄像头视频
读取摄像头内容,然后保存一段十秒钟的视频. import cv2 cameraCapture = cv2.VideoCapture(0) fps = 30 # an assumption size = ...
- MySQL Windows ZIP 免费安装和启动设置
MySQL Windows ZIP免安装版,设置和启动的过程事实上挺麻烦的.以下一步一步介绍使用的过程: 1.下载Windows (x86, 64-bit), ZIP Archive: 2.解压zip ...