Developer/Algorithm

[codility_] MaxProductOfThree

JakeLee92 2020. 12. 5. 13:59

app.codility.com/programmers/lessons/6-sorting/max_product_of_three/

 

MaxProductOfThree coding task - Learn to Code - Codility

Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).

app.codility.com

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

import java.util.* ;

class Solution {
    public int solution(int[] A) {
        
        Arrays.sort(A);

// 10 9 8 7 
        //001  : -10 -9 -8 -7 3 

        //111 : -10 -9 3 7 8

        int iResult = 1 ;
        int iReversIndex = A.length - 1;

        iResult = A[0] * A[1] * A[iReversIndex];
        int iTemp111 = A[iReversIndex-2] * A[iReversIndex-1] * A[iReversIndex];

        if(iResult < iTemp111)
            iResult = iTemp111;

        return iResult ;
    }
}