From 6605c2aca0ddbc02f2f10515b90a35740255d4b4 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 21 Sep 2024 13:36:19 +0800 Subject: [PATCH] 20240921 finished. --- src/problem/mod.rs | 3 +- .../p2374_node_with_highest_edge_score.rs | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2374_node_with_highest_edge_score.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9cae9b8..0401e8f 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -241,4 +241,5 @@ mod p1184_distance_between_bus_stops; mod p815_bus_routes; mod p2332_the_latest_time_to_catch_a_bus; mod p2414_length_of_the_longest_alphabetical_continuous_substring; -mod p2376_count_special_integers; \ No newline at end of file +mod p2376_count_special_integers; +mod p2374_node_with_highest_edge_score; \ No newline at end of file diff --git a/src/problem/p2374_node_with_highest_edge_score.rs b/src/problem/p2374_node_with_highest_edge_score.rs new file mode 100644 index 0000000..f884b00 --- /dev/null +++ b/src/problem/p2374_node_with_highest_edge_score.rs @@ -0,0 +1,43 @@ + +/** + * [2374] Node With Highest Edge Score + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn edge_score(edges: Vec) -> i32 { + use std::collections::HashMap; + use std::cmp::Ordering; + + let mut map = HashMap::new(); + + for (i, &v) in edges.iter().enumerate() { + let entry = map.entry(v).or_insert(0); + *entry += i; + } + + *map.iter().max_by(|a, b| { + match a.1.cmp(b.1) { + Ordering::Less => { Ordering::Less } + Ordering::Equal => { b.0.cmp(a.0) } + Ordering::Greater => { Ordering::Greater } + } + }).unwrap().0 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2374() { + assert_eq!(7, Solution::edge_score(vec![1, 0, 0, 0, 0, 7, 7, 5])); + assert_eq!(0, Solution::edge_score(vec![2, 0, 0, 2])); + } +}