-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (28 loc) · 843 Bytes
/
Solution.java
File metadata and controls
31 lines (28 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public int maximumProduct(int[] nums) {
int max1 = Integer.MIN_VALUE,
max2 = Integer.MIN_VALUE,
max3 = Integer.MIN_VALUE,
min1 = Integer.MAX_VALUE,
min2 = Integer.MAX_VALUE;
for (int num : nums) {
if (num >= max1) {
max3 = max2;
max2 = max1;
max1 = num;
} else if (num >= max2) {
max3 = max2;
max2 = num;
} else if (num >= max3) {
max3 = num;
}
if (num <= min1) {
min2 = min1;
min1 = num;
} else if (num <= min2) {
min2 = num;
}
}
return Math.max(max1 * max2 * max3, max1 * min1 * min2);
}
}