Maximum Product Subarray Leetcode

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 Sum Subarray类似,也是考虑什么时候可以取得最大值。sum的话一遇到负数就可以重新考虑,product的话负数*负数可能是最大值,所以要保留局部最大值和局部最小值。

每次考虑最大值的时候,在:

前面的最大值乘这个数本身(这个数是正数),

这个数本身(这个数是负数或者这个数前面的数是负数,这个数本身是正数),

最小值乘这个数(最小值和这个数都是负数),

这三个里面选局部最大值。

每次考虑最小值的时候,在:

前面的最小值乘这个数本身(这个数是正数),

这个书本身(这个数是负数,前面的最小值也是负数),

最大值乘这个数(最大值是正数,这个数是负数),

这三个里面选局部最小值。

public class Solution {
    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int[] curMax = new int[nums.length];
        int[] curMin = new int[nums.length];
        int res = nums[0];
        curMax[0] = nums[0];
        curMin[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            curMax[i] = Math.max(Math.max(curMax[i - 1] * nums[i], nums[i]), curMin[i - 1] * nums[i]);
            curMin[i] = Math.min(Math.min(curMax[i - 1] * nums[i], nums[i]), curMin[i - 1] * nums[i]);
            if (curMax[i] > res) {
                res = curMax[i];
            }
        }
        return res;
    }
}

改良了一下,可以用O(1) space,但是思路是一样的

public class Solution {
    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int res = nums[0];
        int curMax = nums[0];
        int curMin = nums[0];
        int max, min;
        for (int i = 1; i < nums.length; i++) {
            max = Math.max(Math.max(curMax * nums[i], nums[i]), curMin * nums[i]);
            min = Math.min(Math.min(curMax * nums[i], nums[i]), curMin * nums[i]);
            if (max > res) {
                res = max;
            }
            curMax = max;
            curMin = min;
        }
        return res;
    }
}

这个动态规划做的不顺利,以后回顾一下吧。

时间: 2024-10-08 00:50:04

Maximum Product Subarray Leetcode的相关文章

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

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

LeetCode: Maximum Product Subarray &amp;&amp; Maximum Subarray

Maximum Product Subarray Title: 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. 对于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

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

[C++]LeetCode: 96 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. 思路: 这道题和Maximum Subarray解法类似,维护两个变量来动