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.

解法一:代码最少的解法,maintain一个当前最大值和最小值(注意是连续的,因此必然包含i-1)每次traverse到index = i, 更新包括i在内的最大值和最小值,然后update result,这种做法之所以是正确的是因为,我们会找出所有结束在i的可能的subarray, 而i~(0, n-1),因此会遍历到所有可能的解,这种解答是完备的。
public int maxProduct(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        int maxherepre = nums[0];
        int minherepre = nums[0];
        int maxsofar = nums[0];
        int maxhere, minhere;

        for (int i = 1; i < nums.length; i++) {
            maxhere = Math.max(Math.max(maxherepre * nums[i], minherepre * nums[i]), nums[i]);
            minhere = Math.min(Math.min(maxherepre * nums[i], minherepre * nums[i]), nums[i]);
            maxsofar = Math.max(maxhere, maxsofar);
            maxherepre = maxhere;
            minherepre = minhere;
        }
        return maxsofar;
    }
解法二:从原子操作入手具体分析,思路和上面也是一样的,找每个结束在i的subarray, 然后通过分析i位置上的数正负性来分case操作,最后也是update result。
public int maxProduct(int[] nums) {
        if (nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        int result = Integer.MIN_VALUE, premin = 0, premax = 0;
        for (int i = 0; i < nums.length; i++) {
            int max = 0, min = 0;
            if (nums[i] > 0) {
                if (premax > 0) {
                    max = premax * nums[i];
                } else {
                    max = nums[i];
                }
                if (premin < 0) {
                    min = premin * nums[i];
                }
            } else if (nums[i] < 0) {
                if (premax > 0) {
                    min = premax * nums[i];
                } else {
                    min = nums[i];
                }
                if (premin < 0) {
                    max = premin * nums[i];
                } else {
                    max = 0;
                }
            } else {
                max = 0;
                min = 0;
            }
            premax = max;
            premin = min;
            if (premax > 0) {
                result = Math.max(result, premax);
            } else if (premin < 0) {
                result = Math.max(result, premin);
            } else {
                result = Math.max(result, nums[i]);
            }
        }
        return result;
    }
解法三:思路有点类似于subarray sum equals k, 不过在0的处理上有些复杂,代码如下:
public int maxProduct(int[] nums) {
        if (nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        ArrayList<Integer> list = new ArrayList<>();
        int result = 0, index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) list.add(i);
        }
        if (list.size() == 0) {
            return findmax(nums, 0, nums.length - 1);
        }else {
            for (int i = 0; i < list.size(); i++) {
                if (index == list.get(i)) {
                    index++;
                } else {
                    result = Math.max(result, findmax(nums, index, list.get(i) - 1));
                    index = list.get(i) + 1;
                }
                if (i == list.size() - 1) {
                    return result = Math.max(result, findmax(nums, index, nums.length - 1));
                }
            }
            return result;
        }
    }
    public int findmax(int[] nums, int begin, int end) {
        if (begin == end) return nums[begin];
        int result = 1;
        int maxpos = Integer.MIN_VALUE, maxneg = Integer.MAX_VALUE, minneg = Integer.MIN_VALUE;
        while (begin <= end) {
            result *= nums[begin];
            if (result > 0) {
                if (result > maxpos) maxpos = result;
            } else {
                if (result > minneg) minneg = result;
                if (result < maxneg) maxneg = result;
            }
            begin++;
        }
        return Math.max(maxpos, maxneg / minneg);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值