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: ...
随机推荐
- 微信公众号接口配置验证URL
/** * 审核填写的URL */ public function checkUrl(){ //获得参数 signature nonce token timestamp echostr $nonce ...
- Django中下划线的用法介绍(一)
在Django中有相当多的操作是通过双下划线与动作连接起来使用,为了以后更加方便的查找和使用,现在总结以下Django中基本的双下划线操作 比较符:大于--gt 小于--lt 等于--eq 大于等 ...
- css系统学习网站
最近系统学习一下css样式,找到一个不错的网站.http://css.doyoe.com/
- 关于一道面试题【字符串 '1 + (5 - 2) * 3',怎么算出结果为10,'eval'除外】
最近徘徊在找工作和继续留任的纠结之中,在朋友的怂恿下去参加了一次面试,最后一道题目是: 写一个函数,输入一个字符串的运算式,返回计算之后的结果.例如这样的: '1 + (5 - 2) * 3',计算出 ...
- 简单几步优化你的windows,加快开机速度(重装windows之后要做的几件事)
每个人都想要让自己的系统运行得快一些,开机快一些,我就来说说我自己的经验,我使用的系统是windows8.1,当然这有些方法也适用于其他的系统,我每次重装完系统之后第一件事就是下面几步,当然重装系统之 ...
- 设计模式——抽象工厂模式(C++实现)
#include <iostream> #include <string> using namespace std; class STAbstractProductA { pu ...
- redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool
一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...
- 【网络】 应用&传输层笔记
应用层 应用层常用的协议和各自对应的TCP/UDP端口: DNS TCP/UDP 53 HTTP TCP 80 SMTP TCP 25 POP UDP 110 Telnet TCP 23 DHCP U ...
- 【CSS】 CSS 定位
css 定位和浮动 *******本章大量内容copy自w3school********* 定位对于html界面还是很重要的,因为定位会直接影响到用户的视图.对于css而言,定位也比较灵活. 浮动是一 ...
- Algorithm --> n位数去掉k位后找最小数
去掉K位求取最小数 一个n位的数,去掉其中的k位,怎样使留下来的(n-k)位数按原来的前后顺序组成的数最小 例如 8314925去掉4个数,留下125最小,注意有前后顺序要求,要是没有顺序当然是123 ...