LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析
Array
DP
Divide and Conquer
Description:
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
.
这题自己没做出来,对DP算法没有了解过,这次把DP算法简单学习了下,再看这道题就是最基础的对DP的应用(最优化子结构)
Best Solution:
public class Solution {
public int maxSubArray(int[] nums) {
int dp = nums[0];
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
dp = nums[i] + (dp > 0 ? dp : 0);
max = Math.max(max, dp);
}
return max;
}
}
显然,题目寻找的是nums[start]
到nums[end]
的和最大,找出这段子数组即可。
在这道题的解法上,在for
循环里很明显是个动态的、多阶段决策的思想。dp
的确定,是一个递推思想,现在的dp
值由上一个dp
值所决定,由于是找最大和,故dp<0
时,直接舍去dp
当前值,并赋值nums[i]
,这实际上是改变start
值。而max
的确定,是在当前max
和刚得出的dp
取大值,相当于确定了end
。
另一种解法我认为也非常好理解,此处为代码:
public static int maxSubArray(int[] A) {
int maxSoFar=A[0], maxEndingHere=A[0];
for (int i=1;i<A.length;++i){
maxEndingHere= Math.max(maxEndingHere+A[i],A[i]);
maxSoFar=Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
算法详解原文:
algorithm that operates on arrays: it starts at the left end (element A[1]) and scans through to the right end (element A[n]), keeping track of the maximum sum subvector seen so far. The maximum is initially A[0]. Suppose we've solved the problem for A[1 .. i - 1]; how can we extend that to A[1 .. i]? The maximum
sum in the first I elements is either the maximum sum in the first i - 1 elements (which we'll call MaxSoFar), or it is that of a subvector that ends in position i (which we'll call MaxEndingHere).MaxEndingHere is either A[i] plus the previous MaxEndingHere, or just A[i], whichever is larger.
笔者翻译如下:
算法作用于数组:从左边(元素
A[1]
)开始,向右边(直到A[n]
)扫描,找到最大和。最大值初始化为A[0]
,假设我们现在已经计算到A[1 .. i - 1]
,怎么扩展到A[1 .. i]
呢?前i
个元素的最大和应该是前i-1
个元素中已算出的最大和(我们称为MaxSoFar
),或者是到当前位置i
结束算出的新的和(称为MaxEndingHere
)。其中,MaxEndingHere
是A[i]
加之前的MaxEndingHere
和A[i]
中较大的那一个。
可以看出两种算法思路一致,MaxEndingHere
就相当于是dp
,MaxSoFar
也就是求出的max
。
LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 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(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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: ...
随机推荐
- 云计算之路-阿里云上:服务器CPU 100%问题是memcached的连接数限制引起的
非常抱歉,昨天的服务器CPU 100%问题是达到 memcached 的连接数限制引起的,不是阿里云服务器的问题. 之前我们用的是阿里云“云数据库 memcached 版”,上个周末我们换成了自己搭建 ...
- 在linux上安装dotnetcore
dotnet core已经出来有一段时间了,不是什么新名词了.但这个技术,目前还是比较新的,企业也没有普遍应用.它最大的亮点就是跨平台,也就是我们写的c#代码,可以运行在linux上. 在国内学习do ...
- handsontable 合并单元格
<!DOCTYPE html> <html> <head> <title>handsontable demo</title> <met ...
- Hive数据仓库笔记(三)
Joins: Inner joins: hive> SELECT * FROM sales; Joe 2 Hank 4 Ali 0 Eve 3 Hank 2 hive> SELECT * ...
- java ssm框架实现分页功能 (oracle)
java web 实现分页功能 使用框架:ssm 数据库:oracle 话说 oracle 的分页查询比 mysql 复杂多了,在这里简单谈一下: 查询 前十条数据: SELECT * FROM( S ...
- 机器学习实战笔记(Python实现)-09-树回归
---------------------------------------------------------------------------------------- 本系列文章为<机 ...
- UWP 创建动画的极简方式 — LottieUWP
提到 UWP 中创建动画,第一个想到的大多都是 StoryBoard.因为 UWP 和 WPF 的界面都是基于 XAML 语言的,所以实现 StoryBoard 会非常方便. 来看一个简单的 Stor ...
- Spring之<context:property-placeholder location="classpath:... "/>标签路径问题
<context:property-placeholder location="classpath:... "/>中classpath:是src目录下的意思:所以加载 ...
- 用js写动态时钟 2017-03-23
45每隔1秒变一次: 代码如下: <body onLoad="show()" > ------------表示当页面载入时执行该事件,可以没有 <div id ...
- linux的学习之路--(五)bash及其特性
操作系统组成作用shell是离用户最近的程序 shell:外壳 两类 GUI:Gnome,KDE,Xfce CLI:sh, csh,ksh,bash(都是程序,就是功能支持的不同而已) 进程:在每个进 ...