diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 2f6c3e6..2e09c68 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -382,3 +382,5 @@ mod p3292_minimum_number_of_valid_strings_to_form_target_ii; mod p3285_find_indices_of_stable_mountains; mod p3138_minimum_length_of_anagram_concatenation; + +mod p2545_sort_the_students_by_their_kth_score; diff --git a/src/problem/p2545_sort_the_students_by_their_kth_score.rs b/src/problem/p2545_sort_the_students_by_their_kth_score.rs new file mode 100644 index 0000000..003e777 --- /dev/null +++ b/src/problem/p2545_sort_the_students_by_their_kth_score.rs @@ -0,0 +1,40 @@ +/** + * [2545] Sort the Students by Their Kth Score + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn sort_the_students(score: Vec>, k: i32) -> Vec> { + let k = k as usize; + + let mut target_scores: Vec<(i32, usize)> = + score.iter().enumerate().map(|(i, v)| (v[k], i)).collect(); + + target_scores.sort_unstable_by(|a, b| b.0.cmp(&a.0)); + + target_scores + .into_iter() + .map(|x| score[x.1].clone()) + .collect() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2545() { + assert_eq!( + vec![vec![7, 5, 11, 2], vec![10, 6, 9, 1], vec![4, 8, 3, 15]], + Solution::sort_the_students( + vec![vec![10, 6, 9, 1], vec![7, 5, 11, 2], vec![4, 8, 3, 15]], + 2 + ) + ); + } +}