1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class MaximumProductSubarray { public int maxProduct(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int localMax = nums[0]; int localMin = nums[0]; int globalMax = nums[0]; for (int i = 1; i < nums.length; i++) { int tmp = localMax; localMax = Math.max(Math.max(localMax * nums[i], nums[i]), localMin * nums[i]); localMin = Math.min(Math.min(nums[i], localMin * nums[i]), tmp * nums[i]); globalMax = Math.max(globalMax, localMax); } return globalMax; } }
|