We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 063f23a commit 8b1ce9aCopy full SHA for 8b1ce9a
Dynamic Programming/152_Maximum_Product_Subarray.java
@@ -0,0 +1,25 @@
1
+class Solution {
2
+ public int maxProduct(int[] nums) {
3
+ if (nums == null || nums.length == 0) {
4
+ return 0;
5
+ }
6
+
7
+ int max = nums[0];
8
+ int iMax = nums[0], iMin = nums[0];
9
10
+ for (int i = 1; i < nums.length; i++) {
11
+ if (nums[i] < 0) {
12
+ int temp = iMax;
13
+ iMax = iMin;
14
+ iMin = temp;
15
16
17
+ iMax = Math.max(nums[i], iMax * nums[i]);
18
+ iMin = Math.min(nums[i], iMin * nums[i]);
19
20
+ max = Math.max(max, iMax);
21
22
23
+ return max;
24
25
+}
0 commit comments