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.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

--

一维DP, O(n).
假设A(0, i)区间存在k,使得[k, i]区间是以i结尾区间的最大值, 定义为Max[i], 在这里,当求取Max[i+1]时,

if (Max[i] + A[i+1] >0)
  Max[i+1] = Max[i] + A[i+1]

if(Max[i]+A[i+1] <0) #也就是要舍弃

  Max[i+1] = 0

'''
Created on Nov 13, 2014

@author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
'''
class Solution:
    # @param A, a list of integers
    # @return an integer
    def maxSubArray(self, A):
        sum=0
        max=-655356
        for x in A:
            sum+=x
            if(sum>max):
                max=sum
            if(sum<0):
                sum=0
        return max

LEETCODE —— Maximum Subarray [一维DP]的更多相关文章

  1. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  2. LeetCode: Maximum Subarray 解题报告

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

  3. [LeetCode]Maximum Subarray题解

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

  4. [LeetCode] Maximum Subarray 最大子数组

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

  5. [leetcode]Maximum Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...

  6. LeetCode——Maximum Subarray

    Description: Find the contiguous subarray within an array (containing at least one number) which has ...

  7. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

  8. 53. Maximum Subarray (Array; DP)

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

  9. 53. [LeetCode] Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

随机推荐

  1. Nutch2.x

    http://www.micmiu.com/opensource/nutch/nutch2x-tutorial/

  2. C#版的mongodb最新的官方驱动2.4.0版本

    已经升级了mongodb至最新的3.4的版本,我想想也该把驱动升到最新的了,我用的驱动还是1.7的版本呢,之前几次想升级,都是因为升级驱动需要改动的代码太大了,升级的成本很高,所以懒得动,就这么的用了 ...

  3. VS2010里, using System.Data.OracleClient; 不可用

    当我试图去引用System.Data.OracleClient 这个命名空间时,VS 显示不存在 但是在对象浏览器里却可以找到这个命名空间及里边的对象 另外好像也没有区分清楚 using 和Refer ...

  4. class can not be find with platformType:1 step 1

    使用第三方库的时候 (配合cocopods)混合使用一定要注意 为什么会出现这样的问题... 苦苦难为我半天时间 都有想打人的冲动 前天一切正常今天出来个这 原因很简单当使用cocopods的时候默认 ...

  5. Android开源框架:Universal-Image-Loader解析(三)DiskCache

  6. oracle查询包含某个字段的表

    select column_name,table_name,data_type ,data_length,data_precision,data_scale from DBA_TAB_COLUMNS ...

  7. HTML5体验改进的14条军规

    来自公园<HTML5——用新方式创造更好的用户体验>线下活动中来自火速轻应用的技术总监胡敏东的分享.   1. fake 页 - 首屏加速 目标:首屏 3s 以内   因为 71% 的用户 ...

  8. Jqgrid学习API

    JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面是转自其他人b ...

  9. 发布网站时报错:未能将文件xxx复制到xxx,问题处理

    发布时报错提示: 错误 1 未能将文件 UpLoad\images\73CDC40ECCA44550BA8201D2AC187A46.jpg 复制到 obj\Debug\Package\Package ...

  10. 在Android Studio和Android Eclipse 更改现有项目里的SDK版本

    一,在Eclipse下改项目里的SDK的版本方法有几种,都比较简单:1.右键单击项目--->properties---->Resource----->Android在Project ...