1733. 需要教语言的最少人数
This commit is contained in:
parent
5595c4bd16
commit
bb448434ba
80
src/main.rs
80
src/main.rs
|
|
@ -1,47 +1,61 @@
|
|||
mod arr;
|
||||
struct Solution;
|
||||
|
||||
use std::cmp::min;
|
||||
use crate::arr::make_matrix;
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
|
||||
let mut last = 0_i64;
|
||||
let mut forget_last = 0_i64;
|
||||
let mut ans = 1_i64;
|
||||
let modulo = 1_000_000_007;
|
||||
fn get_range(cur: i32, n: i32, delay: i32, forget: i32) -> (usize, usize) {
|
||||
let top = min(cur + forget - 1, n);
|
||||
((cur + delay) as usize, top as usize)
|
||||
pub fn minimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 {
|
||||
let n = n as usize;
|
||||
let mut m = 0;
|
||||
let languages: Vec<HashSet<i32>> = languages
|
||||
.into_iter()
|
||||
.map(|x| x.into_iter().collect())
|
||||
.collect();
|
||||
let mut ans = 1e9 as i32;
|
||||
let mut is_friend = vec![false; friendships.len()];
|
||||
for i in 0..friendships.len() {
|
||||
let ua = friendships[i][0] as usize - 1;
|
||||
let ub = friendships[i][1] as usize - 1;
|
||||
m = max(m,ua);
|
||||
m = max(m,ub);
|
||||
let lang1 = &languages[ua];
|
||||
let lang2 = &languages[ub];
|
||||
let intersection: HashSet<_> = lang1.intersection(&lang2).cloned().collect();
|
||||
if !intersection.is_empty() {
|
||||
is_friend[i] = true;
|
||||
}
|
||||
}
|
||||
let mut dp = vec![0_i64; n as usize];
|
||||
dp[0] = 1;
|
||||
dp[1] = -1;
|
||||
for i in 1..=n as usize {
|
||||
let (start, end) = get_range(i as i32, n, delay, forget);
|
||||
let cur = (last + dp[i - 1]) % modulo;
|
||||
if end >= start {
|
||||
ans = (ans + cur * ((1 + end - start) as i64 % modulo)) % modulo;
|
||||
for lang in 1..n + 1 {
|
||||
let mut added = vec![false; m + 1];
|
||||
let mut temp_ans = 0;
|
||||
for i in 0..friendships.len() {
|
||||
if is_friend[i] {
|
||||
continue;
|
||||
}
|
||||
let ua = friendships[i][0] as usize - 1;
|
||||
let ub = friendships[i][1] as usize - 1;
|
||||
let lang1 = &languages[ua];
|
||||
let lang2 = &languages[ub];
|
||||
if !(lang1.contains(&(lang as i32)) || added[ua]) {
|
||||
added[ua] = true;
|
||||
temp_ans += 1;
|
||||
}
|
||||
if !(lang2.contains(&(lang as i32)) || added[ub]) {
|
||||
added[ub] = true;
|
||||
temp_ans += 1;
|
||||
}
|
||||
}
|
||||
if i > forget as usize {
|
||||
forget_last += dp[i - forget as usize - 1];
|
||||
ans -= forget_last;
|
||||
}
|
||||
if start - 1 < dp.len() {
|
||||
dp[start - 1] += cur;
|
||||
}
|
||||
if end < dp.len() {
|
||||
dp[end] -= cur;
|
||||
}
|
||||
last = cur;
|
||||
ans = min(ans, temp_ans);
|
||||
}
|
||||
while ans < 0 {
|
||||
ans += modulo;
|
||||
}
|
||||
(ans % modulo) as i32
|
||||
ans
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let sl = Solution::people_aware_of_secret(6,2,4);
|
||||
let lang = make_matrix("[[4,7,2,14,6],[15,13,6,3,2,7,10,8,12,4,9],[16],[10],[10,3],[4,12,8,1,16,5,15,17,13],[4,13,15,8,17,3,6,14,5,10],[11,4,13,8,3,14,5,7,15,6,9,17,2,16,12],[4,14,6],[16,17,9,3,11,14,10,12,1,8,13,4,5,6],[14],[7,14],[17,15,10,3,2,12,16,14,1,7,9,6,4]]");
|
||||
let friendships = make_matrix("[[4,11],[3,5],[7,10],[10,12],[5,7],[4,5],[3,8],[1,5],[1,6],[7,8],[4,12],[2,4],[8,9],[3,10],[4,7],[5,12],[4,9],[1,4],[2,8],[1,2],[3,4],[5,10],[2,7],[1,7],[1,8],[8,10],[1,9],[1,10],[6,7],[3,7],[8,12],[7,9],[9,11],[2,5],[2,3]]");
|
||||
let sl = Solution::minimum_teachings(17, lang, friendships);
|
||||
println!("{:?}", sl);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue