Skip to content

Commit 258edcb

Browse files
152. Maximum Product Subarray (java)
1 parent 9617429 commit 258edcb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: solution/0152.Maximum Product Subarray/Solution.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int maxProduct(int[] nums) {
3+
int max[] = new int[nums.length];
4+
int min[] = new int[nums.length];
5+
int res = nums[0];
6+
max[0] = min[0] = nums[0];
7+
for(int i=1;i<nums.length;i++){
8+
int num = nums[i];
9+
max[i] = Math.max(Math.max(max[i-1]*num,min[i-1]*num),num);
10+
min[i] = Math.min(Math.min(min[i-1]*num,max[i-1]*num),num);
11+
res = Math.max(max[i],res);
12+
}
13+
return res;
14+
}
15+
}

0 commit comments

Comments
 (0)