[LeetCode] 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

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.

  • 常规思路
class Solution {
public:
    int maxProduct(int A[], int n) {
        int product = INT_MIN;
        for (int i = 0; i < n; i++)
        {
            int sub = 1;
            for (int j = i; j < n; j++)
            {
                sub *= A[j];
                if (sub > product)
                {
                    product = sub;
                }
            }
        }

        return product;
    }
};

上述算法时间复杂度过高,LeetCode返回Time Limit Exceeded错误提示。

  • 动态规划思路
    由于数组中有负数存在,一个负数乘以一个负数可能得到一个极大的正数,因此需要维护两个局部变量max_localmin_local。转移方程式如下:
    temp = max_local;
    max_local[i] = max(max(max_local * A[i], min_local * A[i]), A[i]);
    min_local[i] = min(min(temp * A[i], min_local * A[i]), A[i]);
  • 实现代码
/*************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/8 21:43
    *  @Status   : Accepted
    *  @Runtime  : 13 ms
*************************************************************/
class Solution {
public:
    int maxProduct(int A[], int n) {
        if (n == 0)
        {
            return 0;
        }

        int max_local = A[0];
        int min_local = A[0];
        int global = A[0];
        for (int i = 1; i < n; i++)
        {
            int temp = max_local;
            max_local = max(max(max_local * A[i], min_local * A[i]), A[i]);
            min_local = min(min(temp * A[i], min_local * A[i]), A[i]);
            global = max(global, max_local);
        }

        return global;
    }
};
目录
相关文章
|
11月前
Leetcode 53.Maximum Subarray
题意简单,给出一个数组,求出其中最大的子数组和。 这种简单题目背后蕴藏着很巧妙的解题方法。其实只需要遍历一次数组就可以求得解。 思路是这样的,你想想看,如果一段子数组的和是负数, 那么这一段子数组不可能是最大和数组的一部分,丢掉重新从下一个位置开始选。
44 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
88 0
LeetCode 414. Third Maximum Number
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
74 0
LeetCode 318. Maximum Product of Word Lengths
LeetCode 239. Sliding Window Maximum
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。 返回滑动窗口最大值。
57 0
LeetCode 239. Sliding Window Maximum
LeetCode 209. Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
112 0
LeetCode 209. Minimum Size Subarray Sum
LeetCode 164. Maximum Gap
给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。 如果数组元素个数小于 2,则返回 0。
75 0
LeetCode 164. Maximum Gap
LeetCode 152. Maximum Product Subarray
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
43 0
LeetCode 152. Maximum Product Subarray
LeetCode 104. Maximum Depth of Binary Tree
给定一颗二叉树,返回其最大深度. 注意:深度从1开始计数.
59 0
LeetCode 104. Maximum Depth of Binary Tree
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String