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. VM virtuaBox异常关机启动不了的解决方案

    事件回放 我的物理机是win7,上面装了一个VM virtualBox,用来装Centos,有天物理机非正常关闭,导致VM virtuaBox异常关机启动不了,如下: 确实找不到这个vm_liang. ...

  2. VMware下利用ubuntu13.04建立嵌入式开发环境之四

    二.telnet.SSH服务器安装与配置 1.telnet 1.1 安装服务器:apt-get install xinetd telnetd 1.2 安装openbsd-inetd:apt-get i ...

  3. css渐变色DIV

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  4. .Net生成HTML的三种方法

    一.在服务器上指定aspx网页,生成html静态页 public partial class Default2 : System.Web.UI.Page { protected void Page_L ...

  5. matlab初学之roundn和round

    文章出处: http://evaevazhuxun.blog.sohu.com/154543859.html http://blog.sina.com.cn/s/blog_a4034b2801012o ...

  6. Java集合运用技巧

    需要唯一吗? 需要:Set 需要制定顺序吗? 需要:TreeSet 不需要:HashSet 但是想要一个和存储一致的顺序(有序):LinkedHashSet 不需要:List 需要频繁增删吗? 需要: ...

  7. 《C++primer》v5 第6章 函数 读书笔记 习题答案

    6.1 实参是在函数调用处填写的参数.形参是在函数体使用的参数. 实参是形参的初始值. 具体参见:http://blog.163.com/zhengguo_li/blog/static/7030148 ...

  8. MySQL数据库9 - 日期与时间函数

    一 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为数 ...

  9. android的apk逆向工程后的文件目录介绍

    一.用压缩软件打开apk,得到五个文件或文件夹 1.META-INF 2.res 3.AndroidManifest.xml 4.classes.dex 5.resources.arsc 二.xml文 ...

  10. 理解python的with语句

    Python’s with statement provides a very convenient way of dealing with the situation where you have ...