Skip to content

Commit b0b38ad

Browse files
committed
finish "152. Maximum Product Subarray". Fix #152
1 parent 12b3c5d commit b0b38ad

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

Diff for: README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,12 @@
763763
//|{leetcode_base_url}/reverse-words-in-a-string/[Reverse Words in a String]
764764
//|{source_base_url}/ReverseWordsInAString.java[Java]
765765
//|Medium
766-
//
767-
//|152
768-
//|{leetcode_base_url}/maximum-product-subarray/[Maximum Product Subarray]
769-
//|{source_base_url}/MaximumProductSubarray.java[Java]
770-
//|Medium
771-
//
766+
767+
|152
768+
|{leetcode_base_url}/maximum-product-subarray/[Maximum Product Subarray]
769+
|{source_base_url}/MaximumProductSubarray.java[Java]
770+
|Medium
771+
772772
//|153
773773
//|{leetcode_base_url}/find-minimum-in-rotated-sorted-array/[Find Minimum in Rotated Sorted Array]
774774
//|{source_base_url}/FindMinimumInRotatedSortedArray.java[Java]

Diff for: docs/0152-maximum-product-subarray.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
== 152. Maximum Product Subarray
2+
3+
这道题的阶梯思路跟另外某个题的解题思路很类似!
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)