1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package class071;
public class Code01_MaximumProductSubarray {
public static int maxProduct(int[] nums) { double ans = nums[0], min = nums[0], max = nums[0], curmin, curmax; for (int i = 1; i < nums.length; i++) { curmin = Math.min(nums[i], Math.min(min * nums[i], max * nums[i])); curmax = Math.max(nums[i], Math.max(min * nums[i], max * nums[i])); min = curmin; max = curmax; ans = Math.max(ans, max); } return (int) ans; }
}
|