Problem

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example

For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.

Note

这是一道简单的动规题目,同步更新min[]数组解决了nums[i]为负数的问题。即使是求最小乘积子序列,也可以通过取res和min[i]的最小值获得。

Solution

public class Solution {
    public int maxProduct(int[] nums) {
        int len = nums.length;
        int[] max = new int[len];
        int[] min = new int[len];
        int res = max[0] = min[0] = nums[0];
        for (int i = 1; i < len; i++) {
            if (nums[i] >= 0) {
                max[i] = Math.max(nums[i], max[i-1]*nums[i]);
                min[i] = Math.min(nums[i], min[i-1]*nums[i]);
            }
            else {
                max[i] = Math.max(nums[i], min[i-1]*nums[i]);
                min[i] = Math.min(nums[i], max[i-1]*nums[i]);
            }
            res = Math.max(res, max[i]);
        }
        return res;
    }
}

linspiration
161 声望53 粉丝