Skip to content

Commit

Permalink
Add problem 2374: Node With Highest Edge Score
Browse files Browse the repository at this point in the history
EFanZh committed Dec 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1a74528 commit 0d171d7
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1766,6 +1766,7 @@ pub mod problem_2368_reachable_nodes_with_restrictions;
pub mod problem_2369_check_if_there_is_a_valid_partition_for_the_array;
pub mod problem_2370_longest_ideal_subsequence;
pub mod problem_2373_largest_local_values_in_a_matrix;
pub mod problem_2374_node_with_highest_edge_score;

#[cfg(test)]
mod test_utilities;
39 changes: 39 additions & 0 deletions src/problem_2374_node_with_highest_edge_score/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn edge_score(edges: Vec<i32>) -> i32 {
let mut sums = vec![0_u32; edges.len()].into_boxed_slice();

(0..).zip(edges).for_each(|(from, to)| sums[to as u32 as usize] += from);

let mut result = 0;
let mut max_sum = 0;

(0..).zip(&*sums).for_each(|(i, &sum)| {
if sum > max_sum {
max_sum = sum;
result = i;
}
});

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn edge_score(edges: Vec<i32>) -> i32 {
Self::edge_score(edges)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
18 changes: 18 additions & 0 deletions src/problem_2374_node_with_highest_edge_score/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod iterative;

pub trait Solution {
fn edge_score(edges: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [(&[1, 0, 0, 0, 0, 7, 7, 5] as &[_], 7), (&[2, 0, 0, 2], 0)];

for (edges, expected) in test_cases {
assert_eq!(S::edge_score(edges.to_vec()), expected);
}
}
}

0 comments on commit 0d171d7

Please sign in to comment.