본문 바로가기

IT/알고리즘

[코딜리티] - Lesson 8.Dominator

안녕하세요 남갯입니다.








public int solution(int[] A) {
// write your code in Java SE 8
int[] B = A.clone();
Arrays.sort(A);
int length = A.length;
if(length == 0){
return -1;
}
int count = 0;
//반이 넘는것이기 때문에 중앙은 항상 최고 값이다.
int cadidate = A[length / 2];
for (int a : A) {
if (a == cadidate) {
count++;
}
}
//반이상이 안넘으면 대표leader가 없다
if (count < (length / 2) + 1) {
return -1;
}
//clone 한곳에서 찾는다.
for (int i = 0; i < B.length; i++) {
if (B[i] == cadidate) {
return i;
}
}
return 0;

}




https://app.codility.com/demo/results/trainingX6V9TB-JFQ/

'IT > 알고리즘' 카테고리의 다른 글

[코딜리티] - Lesson 9.MaxSliceSum  (0) 2019.09.23
[코딜리티] - Lesson 9.MaxProfit  (0) 2019.09.16
[코딜리티] - Lesson 7.StoneWall  (0) 2019.09.09
[코딜리티] - Lesson 7. Nesting  (0) 2019.09.09
[코딜리티] - Lesson 7. Fish  (0) 2019.09.09