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. Linux系统重要快捷键& Shell 常用通配符

    [Tab]:使用Tab键来进行命令补全: [Ctrl+c]:强行终止当前程序: [Ctrl+d]:键盘输入结束或退出终端: [Ctrl+s]:暂定当前程序,暂停后按下任意键恢复运行: [Ctrl+z] ...

  2. Activity Intent相关FLAG介绍

    先首先简单介绍下Task和Activity的关系   Task就像一个容器,而Activity就相当与填充这个容器的东西,第一个东西(Activity)则会处于最下面,最后添加的东西(Activity ...

  3. Hibernate的增删改查

    一.搭建Hibernate开发环境,这里就不说了,直接说环境搭好后的事情. 二.项目的目录结构

  4. Xcode 7如何 免费 真机调试iOS应用

    运行Xcode后,点击菜单中的Preferences…进入Accounts标签,这里选择添加Apple ID: 在弹出的对话框中登入你的Apple ID,没有的话去注册一个就是了,登录成功后会看到下面 ...

  5. 算法-QuickSort

    #include <stdlib.h> #include <iostream> #include <vector> using namespace std; tem ...

  6. bak骗子公司

    李波 身份证:310101197510313215 手机:13916407777 18621624812 13916821206住址:上海QQ:87766938 沪EE5781 奥迪Q7李寻欢77 s ...

  7. Android 完整开源应用,完整开源项目

    (Antox)聊天的  (new)   (OpenKeychain)OpenPGP在android上的实现  (new)   (Flock)提供同步服务   (OpenFlappyBird)曾经火爆的 ...

  8. 每日总结 -----把人家代码干掉了 我恨git

    今天搞了下午git,写完代码commit之后,pull完发现没法push,说是和origin有分支,然后自己查资料又是reset又是rebase的,commit之后发现自己改动的代码几乎没有被提交上去 ...

  9. 没有为扩展名.htm注册的生成提供程序,没有为扩展名.html注册的生成提供程序

    在web.config中添加下面这段 代码如下 <buildProviders> <add extension=".html" type="System ...

  10. 将时区格式的时间转换为易于阅读的标准格式"yyyy-MM-dd"

    Date的显示格式就是时区格式, String 标准格式 = new SimpleDateFormat("yyyy--MM--dd").format(new Date());