rs: add «506. Relative Ranks»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
01c0dbb83a
commit
4cb30e4b1b
1 changed files with 30 additions and 0 deletions
30
rs/relative-ranks.rs
Normal file
30
rs/relative-ranks.rs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
fn pos_to_string(pos: usize) -> String {
|
||||||
|
match pos {
|
||||||
|
0 => String::from("Gold Medal"),
|
||||||
|
1 => String::from("Silver Medal"),
|
||||||
|
2 => String::from("Bronze Medal"),
|
||||||
|
_ => format!("{}", pos + 1),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_relative_ranks(score: Vec<i32>) -> Vec<String> {
|
||||||
|
let mut heap = BinaryHeap::new();
|
||||||
|
|
||||||
|
for (i, s) in score.into_iter().enumerate() {
|
||||||
|
heap.push((s, i));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut ranks = vec![String::new(); heap.len()];
|
||||||
|
|
||||||
|
let mut i = 0;
|
||||||
|
while let Some((_, idx)) = heap.pop() {
|
||||||
|
ranks[idx] = Self::pos_to_string(i);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ranks
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue