Maximum Product Subarray (3)

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.

标签

数组  dp

联想到最大连续子数组和O(n)的dp代码,我敏锐的感觉到这个求最大连续字数组乘积的问题同样也可以做到O(n).先贴出求最大连续字数组和的代码:

 1 int max_sum(int a[],int n){
 2     int b,sum;
 3     sum=a[0];//这样就能应对所有特殊情况了,例如首项为负数
 4     b=0;
 5     int i;
 6     for(i=0;i<n;i++){
 7         if(b<0)//如果连续项的和小于0,那么要将前面项的和抛弃
 8             b=a[i];
 9         else
10             b+=a[i];//反之,继续求连续项的和
11         if(b>sum)//保证sum是最大的连续项的和
12             sum=b;
13
14     }
15     return sum;//返回最大子数组和
16 }

那么怎么样举一反三,解决好这个问题呢?

根据dp的思路,我们先思考一下我们需要用于存储的局部变量的个数。一个是显然不够的。一个连续最大乘积的可能源于上一个最大的乘积,也可能来自于最小的那个乘积(值为负值,负负得正了!),这个开端开好了,下面就顺理成章了!

先贴代码:

 1 int max3(int a,int b,int c){
 2     if(a<b)
 3         a=b;
 4     if(a<c)
 5         a=c;
 6     return a;
 7 }
 8 int min3(int a,int b,int c){
 9     if(a>b)
10         a=b;
11     if(a>c)
12         a=c;
13     return a;
14 }
15 class Solution {
16 public:
17     int maxProduct(int a[], int n) {
18         if(n==1)
19             return a[0];
20         int max;
21         int min;
22         int i;
23         max=min=a[0];
24         int end=max;
25         for(i=1;i<n;i++){
26             int max1;//用临时变量存储max,因为29行调用可能会覆盖max的值,造成30行运算错误
27             max1=max;
28
29             max=max3(a[i],max1*a[i],min*a[i]);//max取值为a[i],max1*a[i],min*a[i]中最大的
30             min=min3(a[i],max1*a[i],min*a[i]);//min取值为a[i],max1*a[i],min*a[i]中最小的
31             if(max>end)//保证max存储最大的连续字数组乘积
32                 end=max;
33         }
34
35         return end;
36     }
37 };
时间: 2024-08-24 13:53:59

Maximum Product Subarray (3)的相关文章

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

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

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

LeetCode 152. 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. 题目标签:Array, Dynamic Programming 题目给了我们一个nu

【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

题目: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] Binary Tree Maximum Path Sum(递归)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

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

一.题目说明 题目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;