LEETCODE —— Maximum Subarray [一维DP]
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.
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]的更多相关文章
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...
- LeetCode——Maximum Subarray
Description: Find the contiguous subarray within an array (containing at least one number) which has ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- 53. Maximum Subarray (Array; DP)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 53. [LeetCode] Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
随机推荐
- RHEL6 --部署phpMyAdmin与论坛系统
一.rpm安装LAMP平台部署phpMyAdmin 二.搭建wordpress个人博客系统 三.搭建论坛系统 一.rpm安装LAMP平台及部署phpMyAdmin 1.phpMyAdmin简介及获取方 ...
- sql inner join , left join, right join , union,union all 的用法和区别
Persons 表: Id_P LastName FirstName Address City 1 Adams John Oxford Street London 2 Bush George Fift ...
- sqlite的ef使用小结
最近有一个小项目,老师推荐我用下sqlite这种轻型的数据库来进行数据的存储.轻型数据库具有其独特之处:方便,不用安装特定的软件就能够实用,关于sqlite的优点我不赘述,网上还是有好多资料的. 但我 ...
- html input节点很多 json字符串提交解决方法
遇到一个页面,38个input节点,页面前端写好的,不太容易改成 js框架 容易操作的样式,只能自己想办法一个一个id获取然后 setvalue getvalue(miniui): 38个一个一个写太 ...
- 第二章 搭建Android开发环境
这一章为我们讲解了如何搭建Android开发环境. 首先要了解的是Android底层开发需要哪些工具:搭建android应用程序开发环境.android NDK开发环境和交叉编译环境,前两个用来测试L ...
- 布局容器layout Container
画布canvas 盒子Box VBox Hbox-->HGroup VGroup 控制条 ControlBar
- 初学AOP
src\dayday\Count.java package dayday;import org.springframework.stereotype.Component;/** * Created b ...
- Qt中文乱码解决思路
最近项目中遇到不少的Qt中文乱码的问题,主要原因是客户的需求比较多,Qt版本有用4的版本的也有用5的版本,并且还有windows与linux跨平台的需求.经常出现个问题是windows的解决了,源代码 ...
- Eclipse使用指定JDK,无需配置Path变量
修改Eclipse安装目录下的eclipse.ini配置文件 将下面内容添加到文件的首部 -vmF:/Lunatic/IDE/JDK/jdk1.7.0_67/jdk1.7.0_67/bin/javaw ...
- shader forge卡通渲染!
自从用了shader forge,妈妈我再也不写shader了...... 写了3种,分别用的顶点法线.法线贴图.顶点法线+法线贴图,然后还有自发光和受光两种模式,那就是6种了吧... 最后来一张sh ...