刷题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;i<len;i++){
                int p = nums[i];
                if(p>product) product = p;
                for(int t=i+1;t<len;t++){
                    p *= nums[t];
                    if(p>product) product = p;
                }
            }
            return product;
        }
    private:
        int product;
};

性能如下:

Runtime: 200 ms, faster than 6.16% of C++ online submissions for Maximum Product Subarray.
Memory Usage: 9.1 MB, less than 82.50% of C++ online submissions for Maximum Product Subarray.

三、优化措施

仔细再读读题目,一列整数,上述方法太“通用”,一次循环就可以了。

class Solution{
    public:
        //dp,其中dp[i]表示以第i个元素结尾的最大乘积
        int maxProduct(vector<int>& nums){
            if(nums.size()<=1) return nums[0];
            dpMax = nums[0];
            dpMin = nums[0];
            maxProd = nums[0];
            int len = nums.size();

            for(int i=1;i<len;i++){
                int preMax = dpMax;
                dpMax = max(dpMin*nums[i],max(nums[i],dpMax*nums[i]));
                dpMin = min(dpMin*nums[i],min(preMax*nums[i],nums[i]));
                cout<<"i="<<i<<",dpMax="<<dpMax<<",dpMin="<<dpMin<<"\n";
                maxProd = max(dpMax,maxProd);
            }
            return maxProd;
        }
    private:
        int dpMax;
        int dpMin;
        int maxProd;
};
Runtime: 16 ms, faster than 9.45% of C++ online submissions for Maximum Product Subarray.
Memory Usage: 9.1 MB, less than 75.00% of C++ online submissions for Maximum Product Subarray.

原文地址:https://www.cnblogs.com/siweihz/p/12275502.html

时间: 2024-12-15 17:06:46

刷题152. Maximum Product Subarray的相关文章

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

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

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

[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 152. Maximum Product Subarray --------- java

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. 找出最大的相乘的数字,很简单,代码还可以优化的地方很多.但是速度还可以. publi

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的情况下,

152. Maximum Product Subarray java solutions

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 pu