Skip to main content
added 20 characters in body
Source Link
lealand
  • 1.2k
  • 13
  • 22

TheWhile the approach is clear, the implementation could be made a little more readable IMO, in my opinion, by using two helper methods: int[] getMaxThree(int[] arr), which returns the greatest 3 numbers in decreasing order (from greatest to smallest), and int[] getMinTwo(int[] arr), which returns the smallest 2 numbers in increasing order (even though it's not strictly necessary). By doing so, the main method is reduced to:

The approach is clear, the implementation could be made a little more readable IMO by using two helper methods: int[] getMaxThree(int[] arr) which returns the greatest 3 numbers in decreasing order (from greatest to smallest), and int[] getMinTwo(int[] arr) which returns the smallest 2 numbers in increasing order (even though it's not strictly necessary). By doing so, the main method is reduced to:

While the approach is clear, the implementation could be made a little more readable, in my opinion, by using two helper methods: int[] getMaxThree(int[] arr), which returns the greatest 3 numbers in decreasing order (from greatest to smallest), and int[] getMinTwo(int[] arr), which returns the smallest 2 numbers in increasing order (even though it's not strictly necessary). By doing so, the main method is reduced to:

deleted 42 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
private int[] getMaxThree(int[] arr){
    int[] result = { Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE };
    int len = arr.length;
    int helper;

    for(int i = 0; i < len; i++){
        if(arr[i] >= result[0]){
            result[2] = result[1];
            result[1] = result[0];
            result[0] = arr[i];
        } else if(arr[i] >= result[1]){
            result[2] = result[1];
            result[1] = arr[i];
        } else if(arr[i] >= result[2]){
            result[2] = arr[i];
        }
    }

    return result;
}

private int[] getMinTwo(int[] arr){
    int[] result = { Integer.MAX_VALUE, Integer.MAX_VALUE };
    int len = arr.length;
    int helper;

    for(int i = 0; i < len; i++){
        if(arr[i] <= result[0]){
            result[1] = result[0];
            result[0] = arr[i];
        } else if(arr[i] <= result[1]){
            result[1] = arr[i];
        }
    }

    return result;
}
private int[] getMaxThree(int[] arr){
    int[] result = { Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE };
    int len = arr.length;
    int helper;

    for(int i = 0; i < len; i++){
        if(arr[i] >= result[0]){
            result[2] = result[1];
            result[1] = result[0];
            result[0] = arr[i];
        } else if(arr[i] >= result[1]){
            result[2] = result[1];
            result[1] = arr[i];
        } else if(arr[i] >= result[2]){
            result[2] = arr[i];
        }
    }

    return result;
}

private int[] getMinTwo(int[] arr){
    int[] result = { Integer.MAX_VALUE, Integer.MAX_VALUE };
    int len = arr.length;
    int helper;

    for(int i = 0; i < len; i++){
        if(arr[i] <= result[0]){
            result[1] = result[0];
            result[0] = arr[i];
        } else if(arr[i] <= result[1]){
            result[1] = arr[i];
        }
    }

    return result;
}
private int[] getMaxThree(int[] arr){
    int[] result = { Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE };
    int len = arr.length;

    for(int i = 0; i < len; i++){
        if(arr[i] >= result[0]){
            result[2] = result[1];
            result[1] = result[0];
            result[0] = arr[i];
        } else if(arr[i] >= result[1]){
            result[2] = result[1];
            result[1] = arr[i];
        } else if(arr[i] >= result[2]){
            result[2] = arr[i];
        }
    }

    return result;
}

private int[] getMinTwo(int[] arr){
    int[] result = { Integer.MAX_VALUE, Integer.MAX_VALUE };
    int len = arr.length;

    for(int i = 0; i < len; i++){
        if(arr[i] <= result[0]){
            result[1] = result[0];
            result[0] = arr[i];
        } else if(arr[i] <= result[1]){
            result[1] = arr[i];
        }
    }

    return result;
}
deleted 202 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
public int max_prod_three(int[] A){
    int[] maxThree = getMaxThree(A);
    int[] minTwo = getMinTwo(A);

    return Math.max(maxThree[0] * maxThree[1] * maxThree[2], 
                    maxThree[0] * minTwo[0] * minTwo[1]);
}
public int max_prod_three(int[] A){
    int maxOne = Integer.MIN_VALUE, 
        maxTwo = Integer.MIN_VALUE, 
        maxThree = Integer.MIN_VALUE, 
        minOne = Integer.MAX_VALUE, 
        minTwo = Integer.MAX_VALUE;
    int len = A.length;

    for(int i = 0; i < len; i++){
        if(A[i] >= maxOne){
            maxThree = maxTwo;
            maxTwo = maxOne;
            maxOne = A[i];
        } else if(A[i] >= maxTwo){
            maxThree = maxTwo;
            maxTwo = A[i];
        } else if(A[i] >= maxThree){
            maxThree = A[i];
        } 

        if(A[i] <= minOne){
            minTwo = minOne;
            minOne = A[i];
        } else if(A[i] <= minTwo){
            minTwo = A[i];
        }
    }

    return Math.max(maxOne * maxTwo * maxThree, 
                    maxOne * minOne * minTwo);
}
public int max_prod_three(int[] A){
    int[] maxThree = getMaxThree(A);
    int[] minTwo = getMinTwo(A);

    return Math.max(maxThree[0] * maxThree[1] * maxThree[2], maxThree[0] * minTwo[0] * minTwo[1]);
}
public int max_prod_three(int[] A){
    int maxOne = Integer.MIN_VALUE, 
        maxTwo = Integer.MIN_VALUE, 
        maxThree = Integer.MIN_VALUE, 
        minOne = Integer.MAX_VALUE, 
        minTwo = Integer.MAX_VALUE;
    int len = A.length;

    for(int i = 0; i < len; i++){
        if(A[i] >= maxOne){
            maxThree = maxTwo;
            maxTwo = maxOne;
            maxOne = A[i];
        } else if(A[i] >= maxTwo){
            maxThree = maxTwo;
            maxTwo = A[i];
        } else if(A[i] >= maxThree){
            maxThree = A[i];
        } 

        if(A[i] <= minOne){
            minTwo = minOne;
            minOne = A[i];
        } else if(A[i] <= minTwo){
            minTwo = A[i];
        }
    }

    return Math.max(maxOne * maxTwo * maxThree, maxOne * minOne * minTwo);
}
public int max_prod_three(int[] A){
    int[] maxThree = getMaxThree(A);
    int[] minTwo = getMinTwo(A);

    return Math.max(maxThree[0] * maxThree[1] * maxThree[2], 
                    maxThree[0] * minTwo[0] * minTwo[1]);
}
public int max_prod_three(int[] A){
    int maxOne = Integer.MIN_VALUE, 
        maxTwo = Integer.MIN_VALUE, 
        maxThree = Integer.MIN_VALUE, 
        minOne = Integer.MAX_VALUE, 
        minTwo = Integer.MAX_VALUE;
    int len = A.length;

    for(int i = 0; i < len; i++){
        if(A[i] >= maxOne){
            maxThree = maxTwo;
            maxTwo = maxOne;
            maxOne = A[i];
        } else if(A[i] >= maxTwo){
            maxThree = maxTwo;
            maxTwo = A[i];
        } else if(A[i] >= maxThree){
            maxThree = A[i];
        } 

        if(A[i] <= minOne){
            minTwo = minOne;
            minOne = A[i];
        } else if(A[i] <= minTwo){
            minTwo = A[i];
        }
    }

    return Math.max(maxOne * maxTwo * maxThree, 
                    maxOne * minOne * minTwo);
}
deleted 202 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
Loading
added 11 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
Loading
deleted 242 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
Loading
added 120 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
Loading
added 1513 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
Loading
Source Link
Gentian Kasa
  • 2.1k
  • 16
  • 24
Loading