Skip to content

Commit ea463a4

Browse files
committed
solve maximum product subarray
1 parent bde4810 commit ea463a4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class sora0319 {
2+
public class Solution {
3+
public int maxProduct(int[] nums) {
4+
int result = nums[0];
5+
int max = 1;
6+
int min = 1;
7+
8+
for (int num : nums) {
9+
int tempMax = Math.max(Math.max(max * num, min * num), num);
10+
int tempMin = Math.min(Math.min(max * num, min * num), num);
11+
12+
max = tempMax;
13+
min = tempMin;
14+
15+
result = Math.max(result, max);
16+
}
17+
18+
return result;
19+
}
20+
}
21+
22+
}
23+

0 commit comments

Comments
 (0)