-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotRanking.java
More file actions
42 lines (35 loc) · 1.3 KB
/
RobotRanking.java
File metadata and controls
42 lines (35 loc) · 1.3 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
import java.util.*;
public class RobotRanking {
public static String[] findRelativeRanks(int[] score) {
int n = score.length;
String[] result = new String[n];
// Pair of (score, index)
int[][] scoreWithIndex = new int[n][2];
for (int i = 0; i < n; i++) {
scoreWithIndex[i][0] = score[i];
scoreWithIndex[i][1] = i;
}
// Sort by score in descending order
Arrays.sort(scoreWithIndex, (a, b) -> b[0] - a[0]);
for (int rank = 0; rank < n; rank++) {
int index = scoreWithIndex[rank][1];
if (rank == 0) {
result[index] = "Gold Medal";
} else if (rank == 1) {
result[index] = "Silver Medal";
} else if (rank == 2) {
result[index] = "Bronze Medal";
} else {
result[index] = String.valueOf(rank + 1);
}
}
return result;
}
// Example usage
public static void main(String[] args) {
int[] score1 = {50, 80, 30, 100};
System.out.println(Arrays.toString(findRelativeRanks(score1)));
int[] score2 = {23, 45, 67, 12, 89};
System.out.println(Arrays.toString(findRelativeRanks(score2)));
}
}