LeetCode 0152 Maximum Product Subarray
作者:互联网
1. 题目描述
2. Solution 1
1、思路分析
1》 参考LC53 Maximum Subarray的转态转移方程,得到本问题的状态转移方程:
f[i] = max{f(i-1) * nums[i], nums[i]}
以nums = [5, 6, -3, 4, -3]为例,得f = [5, 30, -3, 4, -3],得到结果为30。而实际上结果应该是nums所有元素的乘积为1080。
问题出在哪里?问题出在最后一个-3所对应的结果值既不是-3,也不是 4-3,而是530(-3)4*(-3),所以得到一个结论: 当前位置的最优解未必是由前一个位置的最优解转移得到的。
2》所以,需要根据正负性进行分类讨论。
f_max[i] = max{f_max[i-1] * nums[i], nums[i]}
f_min[i] = max{f_min[i-1] * nums[i], nums[i]}
细节:1) 当nums[i] < 0时,交换一次f_max[i] 与f_min[i];2) 全局解 res = max{res, f_max[i]}。
3》空间优化
不用数组保存f_max[], f_min[],而使用变量max_ending_here, min_ending_here,全局解用max_so_far保存。
2、代码实现
package Q0199.Q0152MaximumProductSubarray;
public class Solution {
public int maxProduct(int[] nums) {
// store the result that is the max we have found so far
int max_so_far = nums[0];
// max_ending_here/min_ending_here stores the max/min product of
// subarray that ends with the current number nums[i]
for (int i = 1, max_ending_here = max_so_far, min_ending_here = max_so_far; i < nums.length; i++) {
// multiplied by a negative makes big number smaller, small number bigger
// so we redefine the extreums by swapping them
if (nums[i] < 0) {
int tmp = max_ending_here;
max_ending_here = min_ending_here;
min_ending_here = tmp;
}
// max/min product for the current number is either the current number itself
// or the max/min by the previous number times the current one
max_ending_here = Math.max(nums[i], max_ending_here * nums[i]);
min_ending_here = Math.min(nums[i], min_ending_here * nums[i]);
// the newly computed max value is a candidate for our global result
max_so_far = Math.max(max_so_far, max_ending_here);
}
return max_so_far;
}
}
3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)
标签:Product,nums,max,min,so,here,ending,0152,Subarray 来源: https://www.cnblogs.com/junstat/p/16304135.html