152. Maximum Product Subarray

Given an integer array nums, find a subarray that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

EX

Example 1:

Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.


Example 2:

Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.


Constraints:

1 <= nums.length <= 2 * 10^4

-10 <= nums[i] <= 10

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

V1-暴力算法

实现

public class T152_MaximumProductSubarray {

/**
* 最粗暴的算法：
*
* 1. 直接暴力计算。
*
* 直接移动 i, j 两个下标志，然后计算结果。
*
* @param nums
* @return
*/
public int maxProduct(int[] nums) {
int maxResult = nums[0];

for(int i = 0; i < nums.length; i++) {
for(int j = i; j < nums.length; j++) {
int result = calc(nums, i, j);
maxResult = Math.max(result, maxResult);
}
}

return maxResult;
}

/**
* 186 / 189 TEL
*
* @param nums
* @param startIndex
* @param endIndex
* @return
*/
private int calc(int[] nums, int startIndex, int endIndex) {
int result = 1;

for(int i = startIndex; i <= endIndex; i++) {
result *= nums[i];
}

return result;
}

}


V2-暴力优化

实现

public class T152_MaximumProductSubarrayV2 {

/**
* 最粗暴的算法：
*
* https://leetcode.com/problems/maximum-product-subarray/solutions/1609493/c-simple-solution-w-explanation-optimization-from-brute-force-to-dp/
*
* @param nums
* @return
*/
public int maxProduct(int[] nums) {
int maxResult = nums[0];

for(int i = 0; i < nums.length; i++) {
// 大的迭代下初始化
int temp = 1;

for(int j = i; j < nums.length; j++) {
// 计算这个循环中的所有子数组，而不是从头计算
temp *= nums[j];

maxResult = Math.max(temp, maxResult);
}
}

return maxResult;
}

}


V3-dp

实现

public int maxProduct(int[] nums) {
int max = nums[0], min = nums[0], result = nums[0];

for (int i = 1; i < nums.length; i++) {
int maxMulti = max * nums[i];
int minMulti = min * nums[i];
max = Math.max(Math.max(maxMulti, minMulti), nums[i]);
min = Math.min(Math.min(maxMulti, minMulti), nums[i]);

// 更新最大值
result = Math.max(result, max);
}
return result;
}


参考资料

https://leetcode.com/problems/maximum-product-subarray/description/

https://leetcode.com/problems/maximum-product-subarray/solutions/48330/simple-java-code/

https://leetcode.com/problems/maximum-product-subarray/solutions/1608862/java-3-solutions-detailed-explanation-using-image/

https://leetcode.com/problems/maximum-product-subarray/solutions/48230/possibly-simplest-solution-with-o-n-time-complexity/