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: ...
随机推荐
- java 动态代理 , 多看看。 多用用。
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.lang.reflect ...
- CSS中的各种FC
什么是FC? Formatting Context,格式化上下文,指页面中一个渲染区域,拥有一套渲染规则,它决定了其子元素如何定位,以及与其他元素的相互关系和作用. BFC 什么是BFC Block ...
- CSS3字体发光效果
text-shadow 该属性为文本添加阴影效果 text-shadow: h-shadow v-shadow blur color; h-shadow: 水平阴影的位置(阴影水平偏移量),可为负值, ...
- c标准头文件
好多C语言库函数参考还是用的TC的库函数参考,因此特地把现在C语言(C99)标准库函数的24个头文件列表如下:assert.h types.h(C99) signal.h stdlib.h c ...
- POI读写Excel-操作包含合并单元格操作
在上篇博客中写到关于Excel操作解析成相关的类,下面将写入一种Excel对Excel表格读取和写入. 对于Excel表格操作,最重要的是创建workBook.其操作顺序是: 1.获得WorkBook ...
- Readiness 探测 - 每天5分钟玩转 Docker 容器技术(144)
除了 Liveness 探测,Kubernetes Health Check 机制还包括 Readiness 探测. 用户通过 Liveness 探测可以告诉 Kubernetes 什么时候通过重启容 ...
- 初尝Eclipse
一.选择开发工具 1.Eclipse和JDK 我所选用的是Eclipse开发工具,此工具可以用来编译JAVA语言,但windows系统没有内置的JAVA运行环境,所以需要下载JDK,用来配置JAVA的 ...
- 二分查找(Java实现)
二分查找:递归实现 public class BinarySearch { /** * @param arr 代查找的数组,需要有序 * @param left 查找区间的左界限 * @param r ...
- javascript - 个人笔记汇总
1. onSubmit = "return function ()"; 2. <input type="text" name="fname&q ...
- freeMark的入门教程
1.FreeMarker支持如下转义字符: \";双引号(u0022) \';单引号(u0027) \\;反斜杠(u005C) \n;换行(u000A) \r;回车(u000D) \t;Ta ...