leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray

Total Accepted: 33022 Total Submissions: 170218My Submissions

Question Solution

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.

Hide Tags

Array Dynamic Programming

Have you met this question in a real interview?

Yes

No

Discuss

而对于Product Subarray,要考虑到一种特殊情况,即负数和负数相乘:如果前面得到一个较小的负数,和后面一个较大的负数相乘,得到的反而是一个较大的数,如{2,-3,-7},所以,我们在处理乘法的时候,除了需要维护一个局部最大值,同时还要维护一个局部最小值,由此,可以写出如下的转移方程式:

max_copy[i] = max_local[i]
max_local[i + 1] = Max(Max(max_local[i] * A[i], A[i]),  min_local * A[i])

min_local[i + 1] = Min(Min(max_copy[i] * A[i], A[i]),  min_local * A[i])

#include<iostream>
#include<vector>
#include<math.h>
using namespace std;

int maxProduct(vector<int>& nums)
{
	int len=nums.size();
	if(len==1)
		return nums[0];

	int maxm=nums[0];
	int minm=nums[0];
	int re=nums[0];
	for(int i=1;i<len;i++)
	{
		int a=max(max(maxm*nums[i],nums[i]),minm*nums[i]);
		int b=min(min(maxm*nums[i],nums[i]),minm*nums[i]);
		if(a>re)
			re=a;
		if(b>re)
			re=b;
		maxm=a;
		minm=b;
	}
	return re;
}

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

  

时间: 2024-09-28 21:32:24

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

[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解法类似,维护两个变量来动

Maximum Product Subarray动态规划思想

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

刷题152. Maximum Product Subarray

一.题目说明 题目152. Maximum Product Subarray,给一列整数,求最大连续子序列,其乘积最大.难度是Medium! 二.我的解答 这个题目,用双重循环就可以了. class Solution{ public: int maxProduct(vector<int>& nums){ if(nums.size()<=1) return nums[0]; product = INT_MIN; int len = nums.size(); for(int i=0;

LeetCode:Maximum Product Subarray

题目: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. 这道题属于动态规划的题型,

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 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 = 

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode: Maximum Product Subarray &amp;&amp; Maximum Subarray

Maximum Product Subarray Title: 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. 对于Product

152. Maximum Product Subarray(js)

152. Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product

Maximum Product Subarray Leetcode

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 Sum Subarray类似,也是考虑什么时候可以取得最大值