Skip to content

Commit 8f8fbbb

Browse files
authored
Merge pull request neetcode-gh#167 from r1cky0/patch-13
Create 152-Maximum-Product-Subarray.java
2 parents 9d2397c + bc59891 commit 8f8fbbb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int maxProduct(int[] nums) {
3+
int res = nums[0];
4+
int max = 1;
5+
int min = 1;
6+
7+
for (int n: nums) {
8+
int tmp = max * n;
9+
max = Math.max(n, Math.max(tmp, min * n));
10+
min = Math.min(n, Math.min(tmp, min * n));
11+
res = Math.max(res, max);
12+
}
13+
return res;
14+
}
15+
}

0 commit comments

Comments
 (0)