20241229 finished.
This commit is contained in:
parent
c51c93a371
commit
24db1ec028
|
@ -398,3 +398,5 @@ mod p3083_existence_of_a_substring_in_a_string_and_its_reverse;
|
|||
mod p3159_find_occurrences_of_an_element_in_an_array;
|
||||
|
||||
mod p3046_split_the_array;
|
||||
|
||||
mod p1366_rank_teams_by_votes;
|
||||
|
|
65
src/problem/p1366_rank_teams_by_votes.rs
Normal file
65
src/problem/p1366_rank_teams_by_votes.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* [1366] Rank Teams by Votes
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
// submission codes start here
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn rank_teams(votes: Vec<String>) -> String {
|
||||
let m = votes[0].len();
|
||||
let mut map = HashMap::with_capacity(m);
|
||||
|
||||
for vote in votes {
|
||||
for (i, c) in vote.chars().enumerate() {
|
||||
let entry = map.entry(c).or_insert(vec![0; m]);
|
||||
entry[i] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut result: Vec<char> = map.keys().map(|c| c.clone()).collect();
|
||||
|
||||
result.sort_unstable_by(|a, b| {
|
||||
let (a_score, b_score) = (map.get(a).unwrap(), map.get(b).unwrap());
|
||||
|
||||
for (i, j) in a_score.into_iter().zip(b_score.into_iter()) {
|
||||
match i.cmp(j) {
|
||||
Ordering::Equal => {
|
||||
continue;
|
||||
}
|
||||
Ordering::Greater => return Ordering::Less,
|
||||
Ordering::Less => return Ordering::Greater,
|
||||
}
|
||||
}
|
||||
|
||||
a.cmp(b)
|
||||
});
|
||||
|
||||
result.iter().collect()
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1366() {
|
||||
assert_eq!(
|
||||
"ACB".to_owned(),
|
||||
Solution::rank_teams(vec_string!("ABC", "ACB", "ABC", "ACB", "ACB"))
|
||||
);
|
||||
assert_eq!(
|
||||
"XWYZ".to_owned(),
|
||||
Solution::rank_teams(vec_string!("WXYZ", "XYZW"))
|
||||
);
|
||||
assert_eq!(
|
||||
"ZMNAGUEDSJYLBOPHRQICWFXTVK".to_owned(),
|
||||
Solution::rank_teams(vec_string!("ZMNAGUEDSJYLBOPHRQICWFXTVK"))
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user