|
| 1 | +package com.diguage.algorithm.leetcode; |
| 2 | + |
| 3 | +/** |
| 4 | + * = 152. Maximum Product Subarray |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/maximum-product-subarray/[Maximum Product Subarray - LeetCode] |
| 7 | + * |
| 8 | + * Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. |
| 9 | + * |
| 10 | + * .Example 1: |
| 11 | + * [source] |
| 12 | + * ---- |
| 13 | + * Input: [2,3,-2,4] |
| 14 | + * Output: 6 |
| 15 | + * Explanation: [2,3] has the largest product 6. |
| 16 | + * ---- |
| 17 | + * |
| 18 | + * .Example 2: |
| 19 | + * [source] |
| 20 | + * ---- |
| 21 | + * Input: [-2,0,-1] |
| 22 | + * Output: 0 |
| 23 | + * Explanation: The result cannot be 2, because [-2,-1] is not a subarray. |
| 24 | + * ---- |
| 25 | + * |
| 26 | + * @author D瓜哥, https://www.diguage.com/ |
| 27 | + * @since 2020-01-05 07:59 |
| 28 | + */ |
| 29 | +public class MaximumProductSubarray { |
| 30 | + /** |
| 31 | + * Runtime: 1 ms, faster than 97.91% of Java online submissions for Maximum Product Subarray. |
| 32 | + * |
| 33 | + * Memory Usage: 36.4 MB, less than 100.00% of Java online submissions for Maximum Product Subarray. |
| 34 | + * |
| 35 | + * Copy form: https://leetcode.com/problems/maximum-product-subarray/discuss/48230/Possibly-simplest-solution-with-O(n)-time-complexity[Possibly simplest solution with O(n) time complexity - LeetCode Discuss] |
| 36 | + */ |
| 37 | + public int maxProduct(int[] nums) { |
| 38 | + // store the result that is the max we have found so far |
| 39 | + int result = nums[0]; |
| 40 | + // max/min stores the max/min product of |
| 41 | + // subarray that ends with the current number nums[i] |
| 42 | + for (int i = 1, max = result, min = result; i < nums.length; i++) { |
| 43 | + // multiplied by a negative makes big number smaller, small number bigger |
| 44 | + // so we redefine the extremums by swapping them |
| 45 | + if (nums[i] < 0) { |
| 46 | + int temp = max; |
| 47 | + max = min; |
| 48 | + min = temp; |
| 49 | + } |
| 50 | + // max/min product for the current number is either the current number itself |
| 51 | + // or the max/min by the previous number times the current one |
| 52 | + min = Math.min(nums[i], min * nums[i]); |
| 53 | + max = Math.max(nums[i], max * nums[i]); |
| 54 | + |
| 55 | + // the newly computed max value is a candidate for our global result |
| 56 | + result = Math.max(result, max); |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + MaximumProductSubarray solution = new MaximumProductSubarray(); |
| 63 | + int[] n1 = {2, 3, -2, 4}; |
| 64 | + int r1 = solution.maxProduct(n1); |
| 65 | + System.out.println((r1 == 6) + " : " + r1); |
| 66 | + |
| 67 | + int[] n2 = {-2, 0, -1}; |
| 68 | + int r2 = solution.maxProduct(n2); |
| 69 | + System.out.println((r2 == 0) + " : " + r1); |
| 70 | + } |
| 71 | +} |
0 commit comments