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. Nginx负载均衡——扩展功能(NGINX Plus)

    本文主要是介绍了NGINX Plus的相关功能,横跨了NGINX Plus R5/R6/R7/R9等各个不同版本的更新. 什么是NGINX Plus? 顾名思义,就是Nginx的加强版或者扩展版.我们 ...

  2. 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

    前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...

  3. Handsontable的前端分页与数据库分页

    Handsontable虽然处理速度很快,但当数据量达到10W+的时候很容易导致浏览器内存泄漏,这时候可以用分页来解决.官网提供了前端分页demo,测试后发现也只能处理低于10W的数据,而且调试的时候 ...

  4. Redis 桌面管理器

    使用Redis桌面管理器,可以方便开发人员进行开发测试,对Redis存储内容进行可视化管理. 下载安装:https://redisdesktop.com/download 1. 为了方便测试,打开re ...

  5. Linux IPMI 配置管理.md

    DELL 服务器 user id 范围:1-16 可以修改用户名和密码 不允许用户名重复 当设置一个已存在的用户名时,无论user id在前或在后,修改密码会将该项用户名设置为空,enable会恢复成 ...

  6. PHP实现发送模板消息到微信公众号

    简述:在这里会具体讲述到如何实现:如何通过后台的代码来实现发送模板消息到已经关注了"心想"公众号的用户. (本人新手,目前实习中,我的所有文档都是在自己开发过程中的记录,有些言语跟 ...

  7. 写了个批量查询qs的软件

    因为需要,自己写了个批量查询qs的小软件.从网站中抓出需要的数据,格式化显示: 对字符串进行检测处理,先用Replace函数去掉字符串的空格,再用正则表达式匹配,返回匹配的字符串,如果没有匹配,则返回 ...

  8. Redis搭建多台哨兵

    搭建多台哨兵 完成spring管理多台哨兵 学习redis如何数据持久化如何管理内存 Redis集群搭建 集群测试 Spring管理集群 2 搭建多台哨兵 2.1 搭建步骤 2.1.1 修改6379哨 ...

  9. python爬微信公众号前10篇历史文章(4)-正则表达式RegularExpressionPattern

    正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串.将匹配的子串替换或者从某个串中取出符合某个条件的子串等. Pytho ...

  10. 用JNDI连接数据库

    之前说到了利用Java中的Properties类读取properties配置文件,连接数据库,现在说另一种方法,他们的目的和作用都是一样的,都是为了提高代码的复用性,解决了更改数据库 时还要更改代码的 ...