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. java 动态代理 , 多看看。 多用用。

    import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.lang.reflect ...

  2. CSS中的各种FC

    什么是FC? Formatting Context,格式化上下文,指页面中一个渲染区域,拥有一套渲染规则,它决定了其子元素如何定位,以及与其他元素的相互关系和作用. BFC 什么是BFC Block ...

  3. CSS3字体发光效果

    text-shadow 该属性为文本添加阴影效果 text-shadow: h-shadow v-shadow blur color; h-shadow: 水平阴影的位置(阴影水平偏移量),可为负值, ...

  4. c标准头文件

    好多C语言库函数参考还是用的TC的库函数参考,因此特地把现在C语言(C99)标准库函数的24个头文件列表如下:assert.h types.h(C99)  signal.h  stdlib.h   c ...

  5. POI读写Excel-操作包含合并单元格操作

    在上篇博客中写到关于Excel操作解析成相关的类,下面将写入一种Excel对Excel表格读取和写入. 对于Excel表格操作,最重要的是创建workBook.其操作顺序是: 1.获得WorkBook ...

  6. Readiness 探测 - 每天5分钟玩转 Docker 容器技术(144)

    除了 Liveness 探测,Kubernetes Health Check 机制还包括 Readiness 探测. 用户通过 Liveness 探测可以告诉 Kubernetes 什么时候通过重启容 ...

  7. 初尝Eclipse

    一.选择开发工具 1.Eclipse和JDK 我所选用的是Eclipse开发工具,此工具可以用来编译JAVA语言,但windows系统没有内置的JAVA运行环境,所以需要下载JDK,用来配置JAVA的 ...

  8. 二分查找(Java实现)

    二分查找:递归实现 public class BinarySearch { /** * @param arr 代查找的数组,需要有序 * @param left 查找区间的左界限 * @param r ...

  9. javascript - 个人笔记汇总

    1. onSubmit = "return function ()"; 2.  <input type="text" name="fname&q ...

  10. freeMark的入门教程

    1.FreeMarker支持如下转义字符: \";双引号(u0022) \';单引号(u0027) \\;反斜杠(u005C) \n;换行(u000A) \r;回车(u000D) \t;Ta ...