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. JS 4 新特性:混合属性(mixins)

    Ext JS4的新特征1:混合属性(mixins) 组合是Extjs4的新特性,可用于实现多继承的情况.该属性会以同步方式加载类文件,并实例化该类(译者推理其内部使用Ext.create方法).直接上 ...

  2. MongooseJS 4.6.4 发布,MongoDB 连接包

    MongooseJS 4.6.4  发布了,MongooseJS 是基于 node.js,使用 JavaScript 编程,连接 MongoDB 数据库的软件包,使MongoDB 的文档数据模型变得优 ...

  3. snort installation, configuration and test

    snort installation: https://www.snort.org/#get-started wget https://www.snort.org/rules/snortrules-s ...

  4. Python基础知识之认识字符串

    Python有一个名为“STR”与许多方便的功能(有一个名为“串”,你不应该使用旧的模块),内置的字符串类. 字符串常量可以通过双或单引号括起来,尽管单引号更常用. 反斜杠工作单,双引号内的文字通常的 ...

  5. 用户、角色、权限三者多对多用hibernate的一对多注解配置

    用户.角色.权限三者多对多用hibernate的一对多注解配置 //权限表@Table(name = "p")public class P { @Id @GeneratedValu ...

  6. HSLA颜色

    CSS2中色彩模式只有RGB色彩模式(RGB即RED.Green.BLue)和十六进制模式,为了能支持 透明opacity 的Alpha值,CSS3又增加了RGBA色彩模式(RGBA即RED.Gree ...

  7. SharePoint REST Create Folder

    function createListFolder(siteUrl, listName, foldername) { var serverUrl = _spPageContextInfo.webAbs ...

  8. 研华运动控制卡 SoftMotion 技术简介

    SoftMotion为研华在设备自动化中的一项重要核心技术,相较于过去市面上ASIC 运动控制解决方案,研华自动化团队自主技术开发软件运动控制技术,并且透过现场可程序逻辑门阵列 (FPGA, Fiel ...

  9. Redis的简介与安装(windows)

    1.简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合).zset(sorte ...

  10. 使用 RequireJS 优化 Web 应用前端

    基于 AMD(Asynchronous Module Definition)的 JavaScript 设计已经在目前较为流行的前端框架中大行其道,jQuery.Dojo.MooTools.EmbedJ ...