Maximum Product of Three Numbers LeetCode solution in Java

1
class Solution {
2
public int maximumProduct(int[] nums) {
3
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
4
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
5
for (int n : nums) {
6
if (n <= min1) {
7
min2 = min1;
8
min1 = n;
9
} else if (n <= min2) { // n lies between min1 and min2
10
min2 = n;
11
}
12
if (n >= max1) { // n is greater than max1, max2 and max3
13
max3 = max2;
14
max2 = max1;
15
max1 = n;
16
} else if (n >= max2) { // n lies betweeen max1 and max2
17
max3 = max2;
18
max2 = n;
19
} else if (n >= max3) { // n lies betwen max2 and max3
20
max3 = n;
21
}
22
}
23
return Math.max(min1 * min2 * max1, max1 * max2 * max3);
24
}
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0