152 Maximum Product Subarray 乘积最大子序列

找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
例如, 给定序列 [2,3,-2,4],
其中乘积最大的子序列为 [2,3] 其乘积为 6。
详见:https://leetcode.com/problems/maximum-product-subarray/description/

方法一:

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int size=nums.size();
        if(size==0||nums.empty())
        {
            return 0;
        }
        vector<int> f(size,0),g(size,0);
        int res=nums[0];
        f[0]=nums[0];
        g[0]=nums[0];
        for(int i=1;i<size;++i)
        {
            f[i]=max(nums[i],max(nums[i]*f[i-1],nums[i]*g[i-1]));
            g[i]=min(nums[i],min(nums[i]*g[i-1],nums[i]*f[i-1]));
            res=max(res,f[i]);
        }
        return res;
    }
};

方法二:优化空间复杂度

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int size=nums.size();
        if(size==0||nums.empty())
        {
            return 0;
        }
        int mx=nums[0];
        int mn=nums[0];
        int res=nums[0];
        for(int i=1;i<size;++i)
        {
            int tmax=mx,tmin=mn;
            mx=max(nums[i],max(tmax*nums[i],tmin*nums[i]));
            mn=min(nums[i],min(tmax*nums[i],tmin*nums[i]));
            res=max(res,mx);
        }
        return res;
    }
};

参考:https://www.cnblogs.com/grandyang/p/4028713.html

原文地址:https://www.cnblogs.com/xidian2014/p/8728145.html

时间: 2024-08-28 12:51:13

152 Maximum Product Subarray 乘积最大子序列的相关文章

(Java) LeetCode 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 6. Example 2: Input: [-2,0,-1]

刷题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 (最大乘积子数组)

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】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. 题解: 先暴力解,遍历所有组合,更新最大值.很显然得超时. Solution

[LeetCode] 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 6. Example 2: Input: [-2,0,-1]

LeetCode OJ 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. [题目分析] 给定一个整数数组,找出数组中的一个子序列,使得序列中所有元素的乘积最大

152. Maximum Product Subarray (Array; DP)

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,不同的是 max(max_local*n

C#解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. 分析:这个题目就是让你求连续的子数组的乘积的最大值. (1)在数组中没有0的情况下,