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 ...
随机推荐
- elasticsearch rpm 安装
参考:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-repositories.html Dow ...
- 八皇后java算法
import java.util.Date; public class EightQueen { public static void main(String[] args) { long star ...
- DOM4J的使用
http://blog.csdn.net/redarmy_chen/article/details/12969219 http://blog.csdn.net/wlbing0625/article/d ...
- ResultSet 结果集带回来的一些信息
ResultSet.getMetaData() 得到结果集的结构信息,比如字段数.字段名等. ResultSet.getMetaData().getTableName(1) 就可以返回表名. Resu ...
- 转的: 重绘ListView 修改标题颜色
1.owerDraw 设置为true 2.实现事件 DrawColumnHeader DrawItem DrawSubItem private void listView1_DrawColumnHea ...
- vimperator setting records
vimperator confugration files :highlight Hint color:#000;background:rgb(250,230,150);border-radius:4 ...
- 修改安卓串口蓝牙app问题记录
* 在网上下载的安卓的蓝牙串口app都是基于eclipse的,但往as里边导入时都存在问题. 迫不得已最后我使用的办法还是在as下面新建工程,然后把相关文件导入.不过还是遇到了其他的问题. * 某个蓝 ...
- jQuery获取带点的id元素
一般jQuery获取某个id为elem元素,只需用$('#elem')就行了,但是如果id中不小心包括了'.' ,那么我吗就会发现这时候jQuery就不能获取到这个元素了.但是使用dom原生的getE ...
- BeanUtils.copyProperties和PropertyUtils.copyProperties的使用区别
http://caoyaojun1988-163-com.iteye.com/blog/1871316
- 前端工具-Sublime、WebStorm-快捷方式使用
记录下我工作中使用的编辑软件Sublime和WebStorm用到的快捷方式来水一贴(*^__^*) Sublime是我使用的最长时间的编辑器了,也熟悉了一些快捷键使用. 1.Ctrl + / --- ...