publicstaticintmaxProduct1(int[] nums){ if (nums == null || nums.length == 0) { return0; } res = nums[0]; int last = maxMul(nums, nums.length - 1); res = Math.max(res, last); return res; }
publicstaticintmaxMul(int[] arr, int i){ if (i == 0) { return arr[0]; } int preMax = maxMul(arr, i - 1); int preMin = minMul(arr, i - 1); res = Math.max(res, preMax); return Math.max(preMax * arr[i], Math.max(preMin * arr[i], arr[i])); }
publicstaticintminMul(int[] arr, int i){ if (i == 0) { return arr[0]; } int preMax = maxMul(arr, i - 1); int preMin = minMul(arr, i - 1); return Math.min(preMin * arr[i], Math.min(preMax * arr[i], arr[i])); }
publicstaticintmaxProduct4(int[] nums){ if (nums == null || nums.length == 0) { return0; } int curMax = nums[0]; int curMin = nums[0]; int res = nums[0]; for (int i = 1; i < nums.length; i++) { int max = curMax * nums[i]; int min = curMin * nums[i]; curMax = Math.max(max, Math.max(min, nums[i])); curMin = Math.min(max, Math.min(min, nums[i])); res = Math.max(res, curMax); } return res; }