Skip to content

Commit 10a6c78

Browse files
committed
add solution in rust & golang: 506. Relative Ranks
Signed-off-by: rajput-hemant <[email protected]>
1 parent b456065 commit 10a6c78

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func findRelativeRanks(score []int) []string {
9+
sorted_score := append([]int(nil), score...)
10+
sort.Slice(sorted_score, func(i, j int) bool {
11+
return sorted_score[i] > sorted_score[j]
12+
})
13+
14+
rank := make(map[int]string)
15+
16+
for i, v := range sorted_score {
17+
switch i {
18+
case 0:
19+
rank[v] = "Gold Medal"
20+
case 1:
21+
rank[v] = "Silver Medal"
22+
case 2:
23+
rank[v] = "Bronze Medal"
24+
default:
25+
rank[v] = fmt.Sprintf("%d", i+1)
26+
}
27+
}
28+
29+
result := make([]string, len(score))
30+
for i, v := range score {
31+
result[i] = rank[v]
32+
}
33+
34+
return result
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn find_relative_ranks(score: Vec<i32>) -> Vec<String> {
3+
let mut score_sorted = score.clone();
4+
score_sorted.sort_unstable_by(|a, b| b.cmp(a));
5+
6+
let mut map = HashMap::new();
7+
for (i, s) in score_sorted.iter().enumerate() {
8+
map.insert(s, i + 1);
9+
}
10+
11+
score
12+
.iter()
13+
.map(|s| {
14+
let rank = map.get(s).unwrap();
15+
match rank {
16+
1 => "Gold Medal".to_string(),
17+
2 => "Silver Medal".to_string(),
18+
3 => "Bronze Medal".to_string(),
19+
_ => rank.to_string(),
20+
}
21+
})
22+
.collect()
23+
}
24+
}

0 commit comments

Comments
 (0)