问题:

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的更多相关文章

  1. LeetCode 53. 最大子序和(Maximum Subarray)

    53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...

  2. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  3. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  4. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  5. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  6. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  7. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  8. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  9. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

随机推荐

  1. BZOJ 刷题记录 PART 6

    [BZOJ2709]水的二分加验证.可是好像被读入萎到了... [BZOJ3229]强大的算法见此.被机房的一堆大神"推荐".于是被坑了...写了一个下午... [BZOJ3631 ...

  2. POJ 1300 Door Man - from lanshui_Yang

    Description You are a butler in a large mansion. This mansion has so many rooms that they are merely ...

  3. 鸟哥Linux私房菜知识点总结6到7章

    近期翻看了一本<鸟哥的Linux私房菜>.这是一本基础的书,万丈高楼平地起.会的不多但能够学.这是我整理的一些知识点.尽管非常基础.希望和大家共同交流. 第6章主机规划与磁盘分区 1.在进 ...

  4. 基于Grunt的版本号构建系统新手教程

    作者:zhanhailiang 日期:2014-10-12 1. 安装nodejs,npm,grunt-cli.參见<Windows环境下安装nodejs+npm+grunt-cli工具> ...

  5. 【Android小应用】颈椎保健操Android开源项目

    前段时间在知乎上回答已入 IT 行业的前辈,有哪些关于保护身体健康的知识分享给 IT 新人? ,回复了一张图片,评论里面有知友希望通过程序可以实现,我后面尝试着通过程序实现了效果,现开源出来,大家可以 ...

  6. Nginx特点

    Nginx特点:1,跨平台:Nginx 能够在大多数 Unix like OS编译执行,并且也有Windows的移植版本号.2,配置异常简单:很easy上手.配置风格跟程序开发一样,神一般的配置.3, ...

  7. CentOS 7没有ifconfig命令处理

    新安装CentOS 7 64位后发现查看ip配置的时候没有ifconfig,百度后发现# yum install net-tools软件包即可.

  8. Smarty模板引擎的使用

    Smarty模板引擎的使用 Smarty是PHP中一个基于MVC模式的模板引擎. Download: http://www.smarty.net/download   特点 1.  最快速度的程序开发 ...

  9. How to debug with IntelliJ IDEA + Grails 2.3.x (转)

    问题: 最近访问grails.org,看到grails framework已经发展到2.3.x了,不免想尝尝鲜.下载了最新的grails-2.3.x之后,创建了一个新的grails app. 添加Bo ...

  10. for循环中一个不容小觑的问题

    for(int i=1;i<=100;i++) 作为程序猿,我们很喜欢使用这种for循环. 可是,当中隐含着一个重要的问题. 过多的编程经历可能使我们的思维产生了一些误解,在上面的for循环中, ...