본문 바로가기

카테고리 없음

[코딜리티] - Lesson 6. MaxProductOfThree

안녕하세요 남갯입니다.




import java.util.Arrays;

public class MaxProductOfThree {

//음수 두개의 곱 * 양수의곱
// or 양수 세개의 곱
public int solution(int[] A) {
Arrays.sort(A);
int result = Integer.MIN_VALUE;

if (result < A[A.length - 1] * A[A.length - 2] * A[A.length - 3]) {
result = A[A.length - 1] * A[A.length - 2] * A[A.length - 3];
}

if (result < A[0] * A[1] * A[A.length - 1]){
result = A[0] * A[1] * A[A.length - 1];
}

return result;
}
}

정렬을 한다면 3가지의 경우의 수밖에 없다.


https://app.codility.com/demo/results/trainingR59R53-PAR/