【Leetcode】Maximum Product Subarray

题目链接:https://leetcode.com/problems/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.

思路:

c[i]表示从0~i的数组中 包含nums[i]这个结点的最大乘积。状态转移方程:c[i] = max{nums[i],c[i-1]*nums[i]}

但这样的话,当有负数存在 结果就不对了,因为当前的乘积可能是负数,但后面可能还有负数,负负得正,当前的负数也应该被记录下来。

再加一个数组,用于记录最小乘积,因为当前最小乘积在未来可能会变成最大乘积。

算法

public int maxProduct(int[] nums) {
    int maxProduct = nums[0];
    int max[] = new int[nums.length];// 到i的连续最大和
    int min[] = new int[nums.length];// 到i的连续最小和
    max[0] = nums[0];
    min[0] = nums[0];  

    for (int i = 1; i < nums.length; i++) {
        max[i] = Math.max(nums[i] * min[i - 1], nums[i] * max[i - 1]);// 连续最大和需要考虑最小、最大情况
        max[i] = Math.max(max[i], nums[i]);  

        min[i] = Math.min(nums[i] * min[i - 1], nums[i] * max[i - 1]);// 同理
        min[i] = Math.min(nums[i], min[i]);  

        maxProduct = Math.max(maxProduct, max[i]);
    }
    return maxProduct;
}  
时间: 2024-08-06 07:34:31

【Leetcode】Maximum Product Subarray的相关文章

【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】【Solution】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. [解法] 参考 http://segmentfault.com/blog

【leetcode】Maximum Product of Word Lengths

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

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

[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解法类似,维护两个变量来动

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