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

  1. '''
  2. Created on Nov 13, 2014
  3.  
  4. @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
  5. '''
  6. class Solution:
  7. # @param A, a list of integers
  8. # @return an integer
  9. def maxSubArray(self, A):
  10. sum=0
  11. max=-655356
  12. for x in A:
  13. sum+=x
  14. if(sum>max):
  15. max=sum
  16. if(sum<0):
  17. sum=0
  18. 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. elasticsearch rpm 安装

    参考:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-repositories.html Dow ...

  2. 八皇后java算法

    import java.util.Date; public class EightQueen { public static void main(String[] args) {  long star ...

  3. DOM4J的使用

    http://blog.csdn.net/redarmy_chen/article/details/12969219 http://blog.csdn.net/wlbing0625/article/d ...

  4. ResultSet 结果集带回来的一些信息

    ResultSet.getMetaData() 得到结果集的结构信息,比如字段数.字段名等. ResultSet.getMetaData().getTableName(1) 就可以返回表名. Resu ...

  5. 转的: 重绘ListView 修改标题颜色

    1.owerDraw 设置为true 2.实现事件 DrawColumnHeader DrawItem DrawSubItem private void listView1_DrawColumnHea ...

  6. vimperator setting records

    vimperator confugration files :highlight Hint color:#000;background:rgb(250,230,150);border-radius:4 ...

  7. 修改安卓串口蓝牙app问题记录

    * 在网上下载的安卓的蓝牙串口app都是基于eclipse的,但往as里边导入时都存在问题. 迫不得已最后我使用的办法还是在as下面新建工程,然后把相关文件导入.不过还是遇到了其他的问题. * 某个蓝 ...

  8. jQuery获取带点的id元素

    一般jQuery获取某个id为elem元素,只需用$('#elem')就行了,但是如果id中不小心包括了'.' ,那么我吗就会发现这时候jQuery就不能获取到这个元素了.但是使用dom原生的getE ...

  9. BeanUtils.copyProperties和PropertyUtils.copyProperties的使用区别

    http://caoyaojun1988-163-com.iteye.com/blog/1871316

  10. 前端工具-Sublime、WebStorm-快捷方式使用

    记录下我工作中使用的编辑软件Sublime和WebStorm用到的快捷方式来水一贴(*^__^*) Sublime是我使用的最长时间的编辑器了,也熟悉了一些快捷键使用. 1.Ctrl + /  --- ...