-
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.
Add problem 1733: Minimum Number of People to Teach
- Loading branch information
Showing
4 changed files
with
213 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
100 changes: 100 additions & 0 deletions
100
src/problem_1733_minimum_number_of_people_to_teach/bit_set.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,100 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
struct BitSet { | ||
data: [u64; 8], | ||
} | ||
|
||
impl BitSet { | ||
fn new(data: Vec<i32>) -> Self { | ||
let mut inner_data = [0_u64; 8]; | ||
|
||
for x in data { | ||
let x = x as u32; | ||
let index = x / 64; | ||
let bit = x % 64; | ||
|
||
inner_data[index as usize] |= 1 << bit; | ||
} | ||
|
||
Self { data: inner_data } | ||
} | ||
|
||
fn has_common_bit(&self, other: &Self) -> bool { | ||
self.data.iter().zip(&other.data).any(|(lhs, rhs)| (lhs & rhs) != 0) | ||
} | ||
|
||
fn bits(&self, mut f: impl FnMut(u32)) { | ||
let mut start = 0; | ||
|
||
for &bits in &self.data { | ||
let mut bits = bits; | ||
|
||
while bits != 0 { | ||
f(start + bits.trailing_zeros()); | ||
|
||
bits &= bits - 1; | ||
} | ||
|
||
start += 64; | ||
} | ||
} | ||
} | ||
|
||
impl Solution { | ||
pub fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 { | ||
let languages = languages.into_iter().map(BitSet::new).collect::<Box<_>>(); | ||
|
||
let mut need_to_learn = vec![false; languages.len()].into_boxed_slice(); | ||
let mut language_counts = vec![0_u16; n as u32 as usize].into_boxed_slice(); | ||
let mut total = 0; | ||
let mut max_common_languages = 0; | ||
|
||
for friendship in friendships { | ||
let [x, y]: [_; 2] = friendship.try_into().ok().unwrap(); | ||
let x = x as u32 as usize - 1; | ||
let y = y as u32 as usize - 1; | ||
let x_languages = &languages[x]; | ||
let y_languages = &languages[y]; | ||
|
||
if !x_languages.has_common_bit(y_languages) { | ||
for (i, languages) in [(x, x_languages), (y, y_languages)] { | ||
if let state @ false = &mut need_to_learn[i] { | ||
*state = true; | ||
|
||
total += 1; | ||
|
||
languages.bits(|language| { | ||
let count = &mut language_counts[language as usize - 1]; | ||
|
||
*count += 1; | ||
|
||
max_common_languages = max_common_languages.max(*count); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
i32::from(total - max_common_languages) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 { | ||
Self::minimum_teachings(n, languages, friendships) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/problem_1733_minimum_number_of_people_to_teach/hash_set.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,69 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::HashSet; | ||
use std::convert::TryInto; | ||
|
||
impl Solution { | ||
pub fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 { | ||
let languages = languages | ||
.into_iter() | ||
.map(|languages| { | ||
languages | ||
.into_iter() | ||
.map(|language| language as u16 - 1) | ||
.collect::<HashSet<_>>() | ||
}) | ||
.collect::<Box<_>>(); | ||
|
||
let mut need_to_learn = vec![false; languages.len()].into_boxed_slice(); | ||
let mut language_counts = vec![0_u16; n as u32 as usize].into_boxed_slice(); | ||
let mut total = 0; | ||
let mut max_common_languages = 0; | ||
|
||
for friendship in friendships { | ||
let [x, y]: [_; 2] = friendship.try_into().ok().unwrap(); | ||
let x = x as u32 as usize - 1; | ||
let y = y as u32 as usize - 1; | ||
let x_languages = &languages[x]; | ||
let y_languages = &languages[y]; | ||
|
||
if x_languages.intersection(y_languages).next().is_none() { | ||
for (i, languages) in [(x, x_languages), (y, y_languages)] { | ||
if let state @ false = &mut need_to_learn[i] { | ||
*state = true; | ||
|
||
total += 1; | ||
|
||
for &language in languages { | ||
let count = &mut language_counts[usize::from(language)]; | ||
|
||
*count += 1; | ||
|
||
max_common_languages = max_common_languages.max(*count); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
i32::from(total - max_common_languages) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 { | ||
Self::minimum_teachings(n, languages, friendships) | ||
} | ||
} | ||
|
||
#[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,43 @@ | ||
pub mod bit_set; | ||
pub mod hash_set; | ||
|
||
pub trait Solution { | ||
fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
( | ||
2, | ||
&[&[1] as &[_], &[2], &[1, 2]] as &[&[_]], | ||
&[&[1, 2] as &[_], &[1, 3], &[2, 3]] as &[&[_]], | ||
), | ||
1, | ||
), | ||
( | ||
( | ||
3, | ||
&[&[2], &[1, 3], &[1, 2], &[3]], | ||
&[&[1, 4], &[1, 2], &[3, 4], &[2, 3]], | ||
), | ||
2, | ||
), | ||
]; | ||
|
||
for ((n, languages, friendships), expected) in test_cases { | ||
assert_eq!( | ||
S::minimum_teachings( | ||
n, | ||
languages.iter().copied().map(<[_]>::to_vec).collect(), | ||
friendships.iter().copied().map(<[_]>::to_vec).collect(), | ||
), | ||
expected, | ||
); | ||
} | ||
} | ||
} |