问题:

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. Java生成目录

    Java生成目录 1.说明 推断目录是否存在,假设不存在就创建该目录.并打印其路径.假设存在,打印其路径 2.实现源代码 /** * @Title:BuildFolder.java * @Packag ...

  2. 不错的C++框架: Thrift(2)-传输和网络相关

    不错的C++框架: Thrift(2)-传输和网络相关 - ang639 - 博客频道 - CSDN.NET 不错的C++框架: Thrift(2)-传输和网络相关

  3. Invalid embedded descriptor for ".proto".Dependencies passed (Protobufer)解决办法

    前言 之前开发的时候,发现居然出现了Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the ...

  4. C# 开发Chrome内核浏览器(WebKit.net)

    原文地址:http://www.cnblogs.com/linyijia/p/4045333.html

  5. C#按字节长度截取字符串

    产生这个问题的原因是将Substring方法将双字节的汉字当成一个字节的字符(UCS2字符)处理了,导致长度变短. 两个扩展方法按字节长度截取字符串 /// <summary> /// 根 ...

  6. 9个杀手级 JVM 编程语言

    9个杀手级 JVM 编程语言 Java虚拟机已经不再是仅仅局限在 Java 了,很多语言提供了脚本转换,可以让其他的程序在java虚拟机上运行,这样能够让更多的开发者能够依靠JVM在Java平台上大有 ...

  7. VIM命令集

    Command Action Notes vim file +54 open file and go to line 54 any : command can be run using + on co ...

  8. c/c++中main函数参数讲解

    参考地址: http://blog.csdn.net/cnctloveyu/article/details/3905720 我们经常用的main函数都是不带参数的.因此main 后的括号都是空括号.实 ...

  9. Android-IA Power Manager (S3)

    Sleep and wake up

  10. javaEE jdbc编程步骤

    1.载入数据库驱动(jar文件) //须要下载一个数据库的jar包,并导入对应的JDBC项目中,创建路径! Class.forName("com.mysql.jdbc.Driver" ...