Skip to content

Commit 00331f3

Browse files
committed
Add problem 2924: Find Champion II
1 parent 122c5af commit 00331f3

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,7 @@ pub mod problem_2917_find_the_k_or_of_an_array;
20152015
pub mod problem_2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros;
20162016
pub mod problem_2919_minimum_increment_operations_to_make_array_beautiful;
20172017
pub mod problem_2923_find_champion_i;
2018+
pub mod problem_2924_find_champion_ii;
20182019

20192020
#[cfg(test)]
20202021
mod test_utilities;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn find_champion(n: i32, edges: Vec<Vec<i32>>) -> i32 {
7+
let mut lost = vec![false; n as u32 as usize].into_boxed_slice();
8+
9+
edges.into_iter().for_each(|edge| lost[edge[1] as u32 as usize] = true);
10+
11+
if lost.iter().filter(|&&lost| !lost).count() == 1 {
12+
lost.iter().position(|&lost| !lost).unwrap() as _
13+
} else {
14+
-1
15+
}
16+
}
17+
}
18+
19+
// ------------------------------------------------------ snip ------------------------------------------------------ //
20+
21+
impl super::Solution for Solution {
22+
fn find_champion(n: i32, edges: Vec<Vec<i32>>) -> i32 {
23+
Self::find_champion(n, edges)
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
#[test]
30+
fn test_solution() {
31+
super::super::tests::run::<super::Solution>();
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn find_champion(n: i32, edges: Vec<Vec<i32>>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((3, &[[0, 1], [1, 2]] as &[_]), 0),
14+
((4, &[[0, 2], [1, 3], [1, 2]]), -1),
15+
];
16+
17+
for ((n, edges), expected) in test_cases {
18+
assert_eq!(S::find_champion(n, edges.iter().map(Vec::from).collect()), expected,);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)