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. anaconda的fish shell支持

    最近在用fish shell,但是无法使用conda的activate命令来激活环境.官方给的有解决方案 https://github.com/conda/conda/blob/5b97a96d78e ...

  2. 运用socket实现简单的ssh功能

    在python socket知识点中已经对socket进行了初步的了解,那现在就使用这些知识来实现一个简单的ssh(Secure Shell)功能. 首先同样是建立两个端(服务器端和客户端) 需求是: ...

  3. 【OpenCV-Python:实现人脸、人眼、嘴巴识别】实战(一)

    AI时代的到来,手机上的APP开始应用人脸识别去完成事情,如iphoneX的人脸解锁,百度自动贩卖机的人脸识别系统进行支付,支付宝的人脸识别登录等,提高了使用软件的易用性,但也因为其便利性,在某些市面 ...

  4. unable to apply changes:plugins "App links assistant",firebase services won'

    新安装的android studio工具报错,本来以为只要在plugins中安装android support插件的,现在点击该插件还是不能apply,提示: unable to apply chan ...

  5. python解决图的最短路径问题

    在hihoCoder上遇到一个算法题目,描述如下: 对图结构有了解的不难发现,这是经典的求图的最短路径问题.以下是python代码: def findMin(row): minL = max(row) ...

  6. load vs. initialize

    这篇文章来对比一下NSObject类的两个方法,+load与+initialize. + (void)load; Invoked whenever a class or category is add ...

  7. 需掌握 - JAVA算法编程题50题及答案

    [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题publi ...

  8. Java CAS机制详解

    CAS目的: 在多线程中为了保持数据的准确性,避免多个线程同时操作某个变量,很多情况下利用关键字synchronized实现同步锁,使用synchronized关键字修可以使操作的线程排队等待运行,可 ...

  9. java判断用户输入的是否至少含有N位小数

    判断用户输入的是否至少含有N位小数. 1.当用户输入的是非数字时抛出异常,返回false. 2.当用户输入数字是,判断其数字是否至少含有N位小数,如果不含有,返回false. 3.当用户输入的数字的小 ...

  10. laravel-Policy步骤

    用户授权Policy 定义策略类 php artisan make:policy <name> 定义方法 注册策略类和模型关联 app > Providers > AuthSe ...