Maximum Product of Three Numbers LeetCode solution in Java
1class Solution {2public int maximumProduct(int[] nums) {3int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;4int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;5for (int n : nums) {6if (n <= min1) {7min2 = min1;8min1 = n;9} else if (n <= min2) { // n lies between min1 and min210min2 = n;11}12if (n >= max1) { // n is greater than max1, max2 and max313max3 = max2;14max2 = max1;15max1 = n;16} else if (n >= max2) { // n lies betweeen max1 and max217max3 = max2;18max2 = n;19} else if (n >= max3) { // n lies betwen max2 and max320max3 = n;21}22}23return Math.max(min1 * min2 * max1, max1 * max2 * max3);24}25}