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)。其中,MaxEndingHereA[i]加之前的MaxEndingHereA[i]中较大的那一个。

可以看出两种算法思路一致,MaxEndingHere就相当于是dpMaxSoFar也就是求出的max

LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  3. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  4. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  5. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  6. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  8. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  10. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

随机推荐

  1. 大文件视频断点续传插件resumabel.js,优化上传速度,缩短最后一片等待时长。

    在angular中使用resumable.js遇到的一个问题:大视频上传到99-100%时,此时正在上传最后一片,最后一片的xhr一直是pending状态.原因插件会检查第一片和最后一片的元数据,检测 ...

  2. java抽象类注意问题

    当知道一个类的子类将不同的实现某个方法时,把该类声明为抽象类很有用,可以共用相同的父类方法,不必再定义. 抽象类和抽象方法的关系:含有抽象方法的类一定是抽象类,抽象类里不一定含有抽象方法. 抽象类存在 ...

  3. Lintcode227 Mock Hanoi Tower by Stacks solution 题解

    [题目描述] In the classic problem of Towers of Hanoi, you have 3 towers and N disks of different sizes w ...

  4. EasyUI动态加载panel,并给panel添加内容

    例子: 给布局内动态添加一个panel,给panel一个id,加内容的时候加到这个id里就可以了 var str=$('<div> <textarea id="contex ...

  5. 纯代码实现wordpress文章隐藏内容评论可见

    在很多网站上都看过这个效果,比如说知己知彼网站,他的部分资源是需要我们评论后才能下载的,那么这个到底有什么用呢,对我而言,除了拿来装逼,还可以增加我的评论数量,不多说,先看看效果: 其实WordPre ...

  6. 线程实现ServerSocket和Socket实现数据交互

    定义一个MyServer类 import java.io.IOException;import java.net.ServerSocket; public class MyServer { publi ...

  7. js制作列表滚动(有滚动条)

    function mouseWheel(obj, fn){ var ff = navigator.userAgent.indexOf("Firefox"); if (ff != - ...

  8. C语言第九次博客作业--指针

    一.PTA实验作业 题目1:两个4位正整数的后两位互换 1. 本题PTA提交列表 2. 设计思路 定义循环变量i,两个数组a[4],b[4] for i=0 to 3 a[i]*p取各个位 *p/=1 ...

  9. Eclipse设置新建jsp文件默认模板

    没有需求就没有进步,遇到问题:现在有大量的html模板页面,但是这些模板是不能和后台进行数据交互的,所以要把他们通通变成jsp页面(59个html文件),还有就是html文件转换成jsp文件的时候,前 ...

  10. NodeJs的async

    async.auto最强大的一个api,它适合逻辑复杂的代码,代码中你一部分需要串行,两部分相互依赖,一部分又需要并行,代码中不需要依赖,这个时候你就可以通过auto随性所欲控制你的代码逻辑. var ...