(Java) 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]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.


暴力解法应该会超时吧,直接考虑用动态规划。用dp[i]来存储以nums[i]为结尾的数组的子序列的最大乘积。有一个问题是根据dp[i-1]求dp[i]时,需要判断nums[i]对全局的影响。因为符号对乘法的影响会使结果不能作为局部最优解,但仍然保留着成为全局最优解的可能。所以同时需要用两个变量存储当前最大的正数乘积,以及最小的负数乘积。当遍历到nums[i]时需要同时更新dp[i],maxPositive以及minNegative。具体的递推是,更新maxPositive,minNegative,之后dp[i]取dp[i-1]和maxPositive中的最大值。更新最大正乘积以及最小负乘积时要记得考虑只有元素自己的情况,并且要分别独立更新。当然本题不需要存储dp[0] ~ dp[i-1],因为dp[i]严格的只和dp[i-1]有关,所以只需要一个dp来存储上一个位置即可,空间可以优化成常数。



Java

class Solution {
    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int maxP = nums[0], minN = nums[0], dp = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int locMax = nums[i] * maxP, locMin = nums[i] * minN;
            maxP = Math.max(nums[i], Math.max(locMax, locMin));
            minN = Math.min(nums[i], Math.min(locMax, locMin));
            dp = Math.max(dp, maxP);
        }
        return dp;
    }
}

原文地址:https://www.cnblogs.com/tengdai/p/9281299.html

时间: 2024-08-09 00:32:13

(Java) LeetCode 152. Maximum Product Subarray —— 乘积最大子序列的相关文章

152 Maximum Product Subarray 乘积最大子序列

找出一个序列中乘积最大的连续子序列(该序列至少包含一个数).例如, 给定序列 [2,3,-2,4],其中乘积最大的子序列为 [2,3] 其乘积为 6.详见:https://leetcode.com/problems/maximum-product-subarray/description/ 方法一: class Solution { public: int maxProduct(vector<int>& nums) { int size=nums.size(); if(size==0|

[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 (最大乘积子数组)

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

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

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

[动态规划] leetcode 152 Maximum Product Subarray

problem:https://leetcode.com/problems/maximum-product-subarray 类似买卖股票,需要维护两个状态,当前最大数和最小数. class Solution { public: int maxProduct(vector<int>& nums) { int res = INT_MIN; int num_max = 1; int num_min = 1; for(int i = 0;i<nums.size();i++) { int

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;