【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 1 (TLE)

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int n = nums.size(), mproduct = nums[0];
        for (int i = 0; i < n; ++i) {
            int tmp = nums[i];
            mproduct = max(mproduct, tmp);
            for (int j = i + 1; j < n; ++j) {
                tmp = tmp * nums[j];
                mproduct = max(mproduct, tmp);
            }
        }
        return mproduct;
    }
};

  Besides keeping track of the largest product, we also need to keep track of the smallest product. Why? The smallest product, which is the largest in the negative sense could become the maximum when being multiplied by a negative number. (from here)

  Let us denote that:

f(k) = Largest product subarray, from index 0 up to k.

  Similarly,

g(k) = Smallest product subarray, from index 0 up to k.

  Then,

f(k) = max( f(k-1) * A[k], A[k], g(k-1) * A[k] )
g(k) = min( g(k-1) * A[k], A[k], f(k-1) * A[k] )

Solution 2 ()

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        int maxPro = nums[0], minPro = nums[0], result = nums[0], n = nums.size();
        for (int i=1; i<n; i++) {
            int mx = maxPro, mn = minPro;
            maxPro = max(max(nums[i], mx * nums[i]), mn * nums[i]);
            minPro = min(min(nums[i], mx * nums[i]), mn * nums[i]);
            result = max(maxPro, result);
        }
        return result;
    }
};
时间: 2024-10-14 04:56:21

【LeetCode】152. Maximum Product Subarray的相关文章

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

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

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

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

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

Java for 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. 解题思路: 计算连续的积最大,由于会有负数出现,因此需要用两个int表示包含num