-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add problem 2374: Node With Highest Edge Score
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/problem_2374_node_with_highest_edge_score/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |