-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCT_2.java
More file actions
56 lines (49 loc) · 1.95 KB
/
CT_2.java
File metadata and controls
56 lines (49 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @introduce
*
* 토너먼트 방식의 경기에서 각 플레이어가 마지막으로 참가한 라운드를 결정
* N명의 플레이어가 있으며, 각 플레이어는 고유한 번호와 실력 수준을 가짐
*
* 1. 플레이어는 0번 인덱스부터 N - 1번 인덱스까지 있음
* 2. 각 플레이어의 실력은 skills[K]로 표현
* 3. 두 플레이어가 경기하면 더 높은 실력을 가진 플레이어가 승리
* 4. 이 과정을 반복하여 마지막 승자가 결정될 때까지 진행
*
* 목표: 각 플레이어가 마지막으로 탈락한 라운드를 구하는 것
*/
class Solution {
public int[] solution(int[] skills) {
int n = skills.length;
int rounds = (int) (Math.log(n) / Math.log(2));
int[] results = new int[n];
int[] currentRoundSkills = Arrays.copyOf(skills, n);
// 대회 진행
for (int round = 1; round <= rounds; round++) {
for (int i = 0; i < n; i += 2) {
if (currentRoundSkills[i] > currentRoundSkills[i + 1]) {
results[skillsToIndex(skills, currentRoundSkills[i + 1])] = round;
currentRoundSkills[i / 2] = currentRoundSkills[i];
} else {
results[skillsToIndex(skills, currentRoundSkills[i])] = round;
currentRoundSkills[i / 2] = currentRoundSkills[i + 1];
}
}
// 다음 라운드 선수 수 반으로 줄이기
n /= 2;
}
// 결승에 간 선수도 라운드 수 채우기
results[skillsToIndex(skills, currentRoundSkills[0])] = rounds;
return results;
}
private int skillsToIndex(int[] skills, int target) {
for (int i = 0; i < skills.length; i++) {
if (skills[i] == target) {
return i;
}
}
return -1;
}
}