ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0152 Maximum Product Subarray

2022-05-24 08:00:28  阅读:203  来源: 互联网

标签:Product nums max min so here ending 0152 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有