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.



思路:类似最大和连续子序列那样,不过除了记录最大乘积,我们还要记录最小的乘积。这里我分三种情况:

1.A[i]>0。

2.A[i]<0。p[i].max=Max(p[i-1].min*A[i],A[i]);也就是看看前i-1项的最小值是不是负数,若是负数,则p[i].max=p[i-1]*A[i](负负得正);若不是负数,则p[i]=A[i]。

3.若A[i]=0,则以0为结尾的序列的乘积的最大值和最小值一定都是0。

代码:

class Solution {
private:
struct pro{
    int max;
    int min;
};
public:
    int Max(int a,int b){
        return a>b?a:b;
    }
    int Min(int a,int b){
        return a<b?a:b;
    }
    int maxProduct(int A[], int n) {
        struct pro p[n];
        p[0].max=A[0];
        p[0].min=A[0];
        for (int i=1;i<n;++i)
        {
            if(A[i]>0){
                p[i].max=Max(p[i-1].max*A[i],A[i]);
                p[i].min=Min(p[i-1].min*A[i],A[i]);
            }
            else if(A[i]<0){
                p[i].max=Max(p[i-1].min*A[i],A[i]);
                p[i].min=Min(p[i-1].max*A[i],A[i]);
            }
            else{
                p[i].max=0;
                p[i].min=0;
            }
        }
        int res=p[0].max;
        for (int i=1;i<n;++i)
        {
            if(p[i].max>res)
                res=p[i].max;
        }
        return res;
    }
};
时间: 2024-10-10 04:07:23

Maximum Product Subarray(最大连续乘积子序列)的相关文章

lintcode 中等题 :Maximum Product Subarray 最大连续乘积子序列

题目 乘积最大子序列 找出一个序列中乘积最大的连续子序列(至少包含一个数). 样例 比如, 序列 [2,3,-2,4] 中乘积最大的子序列为 [2,3] ,其乘积为6. 解题  法一:直接暴力求解 时间复杂度O(N2) public class Solution { /** * @param nums: an array of integers * @return: an integer */ public int maxProduct(int[] nums) { // write your c

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. 此题要求是要在一个无需的数组找出一个乘积最大的连续子数组 例如[2,3,-2,4],

【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 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(枚举)

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

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

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