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