You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.
The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:
- The 1st place athlete's rank is "Gold Medal".
- The 2nd place athlete's rank is "Silver Medal".
- The 3rd place athlete's rank is "Bronze Medal".
- For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x").
Return an array answer of size n where answer[i] is the rank of the ith athlete.
Example 1:
Input: score = [5,4,3,2,1]
Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].
Example 2:
Input: score = [10,3,8,9,4]
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].
랭크 구하는 함수
class Solution {
public String[] findRelativeRanks(int[] score) {
String[] answer = new String[score.length];
// 원본 인덱스를 담는 배열
Integer[] index = new Integer[score.length];
for(int i = 0; i < score.length; i++) {
index[i] = i;
}
// 인덱스 배열을 score 배열 기준 내림차순으로 정렬
Arrays.sort(index, (i1, i2) -> score[i2] - score[i1]);
for(int i = 0; i < index.length; i++) {
//
int idx = index[i];
if(i == 0) {
answer[idx] = "Gold Medal";
} else if(i == 1) {
answer[idx] = "Silver Medal";
} else if(i == 2) {
answer[idx] = "Bronze Medal";
} else {
answer[idx] = (i+1) + "";
}
}
return answer;
}
}
0, 1,2 순으로 금은동메달 정렬 후 출력
728x90
'Study > 코딩스터디_TIL' 카테고리의 다른 글
[java 스터디] 우선순위 큐 (0) | 2025.02.10 |
---|---|
[java] 큐 활용 문제풀이 백준 4949번 균형잡힌 세상 (0) | 2025.02.08 |
[java] 큐 활용 문제풀이 백준 26043번 식당메뉴 (0) | 2025.02.06 |
[JAVA 스터디] Queue 개념 (0) | 2025.02.05 |
[JAVA 스터디] Stack 과 Queue (0) | 2025.02.04 |