leetcode_53题——Maximum Subarray(动态规划)

Maximum Subarray

Total Accepted: 62857 Total Submissions: 182415My Submissions

Question Solution

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.

click to show more practice.

Hide Tags

Divide and Conquer Array Dynamic Programming

别人的思路是:

这道题,如果看过Mark Allen Weiss写的数据结构与算法分析一书,可以发现是第二章为了介绍算法的魅力,逐步从三次方的时间复杂度一直优化到线性时间复杂度的一个例子。有一点小区别,就是书中给的例子简化了,如果全为负数,则认为最大子序列和为0,所以有一点点出入,不过基本思路是完全一样的。

现在对线性时间解法做一下解释,属于一种DP问题。已知了前k个元素的最大子序列和为maxSub(已经被记录下来了),以及一个临时和sum,如果添加了第k+1这个元素,由于是连续子序列这个限制,所以如果k+1这个元素之前的和是小于0的,那么对于增大k+1这个元素从而去组成最大子序列是没有贡献的,所以可以把sum 置0。举个例子,-1, -2 ,4, -5, 7这里假定7为第k+1个元素,那么很明显可以看出,之前的sum = -5 + 4 =-1,那么这样对于7来说只会减少它,所以直接置sum = 0, 0 + 7才能得到正确的答案。再拓展这个数组, -1, -2, 4, -5, 7, 1 这里1之前的sum = 7 > 0,对于后面的1来组成最大子序列是有贡献的,所以sum = 7 + 1 =8。再注意一点,只要sum不减到负数,中间出现小于0的元素是没关系的,sum仍然可以继续累加。

利用动态规划的思想完成,时间复杂度为O(n)。已知0,..,k的最大和以后,0,...k+1的最大和为:

1)若sum[k]>=0,sum[k+1]=sum[k]+A[k+1]。

2)若sum[k]<0,sum[k+1]=A[k+1]。

自己的理解

这其中其实主要注意关键的一点是,在最长的序列和他需要的是连续的,而保持一个前面的0到k的最大值,这个一定包含了第k个值,这里我用了两个变量来维护最大值。即一个是0到k的最大值(不一定包含k),一个是0到k的最大值一定包含k,而第k+1计算的一定是在后面那个值上面计算。

#include<iostream>
#include<vector>
using namespace std;

int maxSubArray(vector<int>& nums){
	if(nums.empty())
		return 0;
	int len=nums.size();
	if(len==1)
		return nums[0];
	int sum0=nums[0];
	int sum1=nums[0];
	for(int i=1;i<len;i++)
	{
		if(sum1>=0)
			sum1=sum1+nums[i];
		else
			sum1=nums[i];
		if(sum1>sum0)
			sum0=sum1;
	}
	return sum0;
}

int main()
{
	int ary[9]={-2,1,-3,4,-1,2,1,-5,4};
	vector<int> vec(ary,ary+2);
	cout<<maxSubArray(vec)<<endl;
}

  

时间: 2024-08-02 13:34:33

leetcode_53题——Maximum Subarray(动态规划)的相关文章

leetcode Maximum Subarray 动态规划实现

问题: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [㈢,1,3,4,ㄢ,2,1,5,4],the contiguous subarray [4,ㄢ,2,1] has the largest sum = 6. 经过多次尝试发现该问题本身没有最优子结构,但是其问题的转化:f(j

53. Maximum Subarray(动态规划 求最大子数组)

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[i] = dp[i-1]+nums[i] ,d

[C++]LeetCode: 96 Maximum Product Subarray(动态规划)

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 思路: 这道题和Maximum Subarray解法类似,维护两个变量来动

Leetcode 动态规划 Maximum Subarray

正整数或一位小数或者俩位小数的正则表达式的写法 ^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$ Leetcode 动态规划 Maximum Subarray,布布扣,bubuko.com

leetcode 刷题之路 83 Maximum Subarray

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. 输入一个整形数组,数组里有正数也有负数,求所有子数组的和的最大

Maximum Product Subarray动态规划思想

该题即是昨天没有做出来的题目,想了很久,想出了一个普通的做法,提交发现超时了.思想是新建一个数组,保存每个元素与后面的元素相乘后得到的最大值,然后再在该数组中选出最大的值,返回.[笨死 发现行不通后决定还是求教度娘了. 果然大神无处不在,该题可运用动态规划思想解决.考虑到正负数相乘后会出现的各种结果,采取保存局部最小和局部最大值的方式.列出公式: int a=localmin*A[i] int b=localmax*A[i] localmin = min(A[i],min(a,b)) local

LeetCode练题——53. Maximum Subarray

1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has th

LeetCode——Maximum Subarray

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. 原题链接: https://oj.leetcode.com/p

最大子数组问题 Maximum Subarray

Maximum Subarray 标签(空格分隔): algorithm 这个问题我们先看下问题的描述: 问题描述 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