leetcode-53-Maximum Subarray(动态规划详解)
题目描述:
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
要完成的函数:
int maxSubArray(vector<int>& nums)
说明:
1、这是一道动态规划的题目。如果我们不使用动态规划,可能就要找出所有的子数组,然后一一判断,这是一件很恐怖的事情。
2、我们先说一下这道题要怎么做,最后再来总结动态规划的本质。
我们要选一个sum最大的子数组,我们碰到一个新的数字,比如处理完-2之后现在碰到1这个新的数字,我们有两种选择:
一是,把1加入到旧有的子数组中,“延续”下来,现在新的sum为-2+1=-1。
二是,废弃掉旧有的子数组,重新开始,以1为第一个元素,现在新的sum为1。
那很明显,我们处理完元素-2现在在处理元素1,我们更想要重新开始,无论之后还有什么元素,重新开始都比加入之前旧有子数组的结果要好。
这是一个局部最优解。
之后我们继续处理下一个元素-3,我们同样有两种选择:
一是,-3加入旧有子数组,现在新的sum为1-3=-2。
二是,重新开始,-3作为第一个元素,现在新的sum为-3。
很明显,我们更想要一的做法,“延续”旧有的子数组。
我们依然得到了当前状态的最优解……
后续照这种做法去做就好了。
代码如下:
int maxSubArray(vector<int>& nums)
{
int local=nums[];//存储每一个阶段的最优解
int global=nums[];//存储整个过程能得到的最大sum
for(int i=;i<nums.size();i++)//从i=1开始
{
local=max(local+nums[i],nums[i]);//选择要“延续”还是“重新开始”
global=max(global,local);
}
return global; }
上述代码实测13ms,beats 61.40% of cpp submissions。
对整个过程还不清晰的同学,不妨照着题目给的例子,自己写一遍程序运行结果,对整个过程会有更加清楚的认识。
3、动态规划的特性。
笔者也没有做过很多关于动态规划的题目,之前也只是接触过类似于viterbi这样的算法,这道题是第一次正儿八经的动态规划题。但我们仍可以管中窥豹,从中总结出动态规划的一些特性。
动态规划是单重循环,只需要从头到尾做一次遍历。
我们把一个过程分为多个阶段,比如题目给的例子,我们要处理的元素1是一个阶段,要处理的元素-3是一个阶段,要处理的元素4是一个阶段……
每个阶段可以由多个状态组成,比如我们要处理的元素1,第一个状态是加入到旧有的子数组,第二个状态是重新开始新的子数组。每个阶段都要选择一个新的状态,构成局部最优解。
我们从头到尾遍历了一遍,每一次都选择了每个阶段的局部最优解,最后我们得到的结果自然也是全局最优解。
关于时间复杂度和空间复杂度,动态规划远远优于“找到所有子数组,然后一一计算”的暴力解法。
假设我们有9个阶段,每个阶段2种状态。(题目给的例子)
使用动态规划算法,时间复杂度可以粗略认为是2+2+……+2+2=2*9=18;空间复杂度,每次只需要保存上一个阶段的局部最优解和当前全局最优解两个参数。
使用暴力解法,时间复杂度,必然要找出所有子数组,单个数字就有9种可能,2个数字的有8种可能,3个数字的有7种可能……已经远远超过动归的做法;空间复杂度方面也会超过动归的做法。
动态规划真是个有趣的算法……
leetcode-53-Maximum Subarray(动态规划详解)的更多相关文章
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- 指令-Directive
restrict:'A'用作设定用那种方式使用指令. 可组合使用如restrict:'AE' E - 元素名称: <my-directive></my-directive> A ...
- MySQL慢日志简介及Anemometer工具介绍
作者:王航威 - fordba.com 来源:http://fordba.com/box-anemometer-visual-mysql-slow.html,叶师傅对原文内容略有调整 备注:王航威是知 ...
- android 常用adb 及linux 命令
一.ADB相关 adb shell:进入连接的USB调试模式设备shell命令行下 adb tcpip 5555:将USB连接的调试及的连接方式改为网络远程模式进行调试 这里端口为5555(adb 默 ...
- 设计带有placeHolder的TextView
设计带有placeHolder的TextView 效果: 源码: PlaceholderTextView.h 与 PlaceholderTextView.m // // PlaceholderText ...
- [翻译] VBFPopFlatButton
VBFPopFlatButton https://github.com/victorBaro/VBFPopFlatButton Flat button with 21 different states ...
- [翻译] ASFTableView
ASFTableView A customizable Web like multi column table view for iOS with header and inner rows. 一个类 ...
- VB ASP 使用 now() 时默认格式调整方法
修改注册表 [HKEY_USERS\.DEFAULT\Control Panel\International] "sShortDate"="yyyy-M-d" ...
- UI(三)
1. 2.经常用到的loadmap函数 void CTopology::LoadMap() { //m_map.RemoveAllLayers(); AddLayersBasemap(); AddLa ...
- C++11 的右值引用
作者:Tinro链接:https://www.zhihu.com/question/22111546/answer/30801982来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- chrome开发者工具那点事
Elements:查找网页源代码HTML中的任一元素,手动修改任一元素的属性和样式且能实时在浏览器里面得到反馈. Console:记录开发者开发过程中的日志信息,且可以作为与JS进行交互的命令行She ...