Compare commits
2 Commits
6605c2aca0
...
b513afc8d2
Author | SHA1 | Date | |
---|---|---|---|
b513afc8d2 | |||
5f58928e71 |
|
@ -2,4 +2,5 @@
|
|||
pub mod util;
|
||||
pub mod problem;
|
||||
pub mod week_385;
|
||||
pub mod week_416;
|
||||
pub mod double_week_125;
|
||||
|
|
|
@ -243,3 +243,4 @@ mod p2332_the_latest_time_to_catch_a_bus;
|
|||
mod p2414_length_of_the_longest_alphabetical_continuous_substring;
|
||||
mod p2376_count_special_integers;
|
||||
mod p2374_node_with_highest_edge_score;
|
||||
mod p997_find_the_town_judge;
|
62
src/problem/p997_find_the_town_judge.rs
Normal file
62
src/problem/p997_find_the_town_judge.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use regex::escape;
|
||||
|
||||
/**
|
||||
* [997] Find the Town Judge
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn find_judge(n: i32, trust: Vec<Vec<i32>>) -> i32 {
|
||||
use std::collections::HashMap;
|
||||
let n = n as usize;
|
||||
|
||||
let mut trust_map = HashMap::with_capacity(n);
|
||||
let mut trusted_map = HashMap::with_capacity(n);
|
||||
|
||||
for i in 1..=n {
|
||||
trust_map.insert(i, 0);
|
||||
}
|
||||
|
||||
for i in trust {
|
||||
let (a, b) = (i[0] as usize, i[1] as usize);
|
||||
|
||||
let entry = trust_map.entry(a).or_insert(0);
|
||||
*entry += 1;
|
||||
|
||||
let entry = trusted_map.entry(b).or_insert(vec![]);
|
||||
entry.push(a);
|
||||
}
|
||||
|
||||
for man in trust_map.iter().filter(|p| *p.1 == 0) {
|
||||
if let Some(people) = trusted_map.get(man.0) {
|
||||
if people.len() == n - 1 && !people.contains(man.0) {
|
||||
return *man.0 as i32;
|
||||
}
|
||||
} else {
|
||||
if n == 1 {
|
||||
return *man.0 as i32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_997() {
|
||||
assert_eq!(2, Solution::find_judge(2, vec![vec![1, 2]]));
|
||||
assert_eq!(3, Solution::find_judge(3, vec![vec![1, 3], vec![2, 3]]));
|
||||
assert_eq!(1, Solution::find_judge(1, vec![]));
|
||||
assert_eq!(-1, Solution::find_judge(2, vec![]));
|
||||
}
|
||||
}
|
2
src/week_416/mod.rs
Normal file
2
src/week_416/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
mod problem1;
|
||||
mod problem2;
|
26
src/week_416/problem1.rs
Normal file
26
src/week_416/problem1.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn report_spam(message: Vec<String>, banned_words: Vec<String>) -> bool {
|
||||
use std::collections::HashSet;
|
||||
|
||||
let mut set = HashSet::with_capacity(banned_words.len());
|
||||
for i in banned_words {
|
||||
set.insert(i);
|
||||
}
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
for i in message.iter() {
|
||||
if set.contains(i) {
|
||||
result += 1;
|
||||
}
|
||||
|
||||
if result >= 2 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
61
src/week_416/problem2.rs
Normal file
61
src/week_416/problem2.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
pub struct Solution;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::BinaryHeap;
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct Worker {
|
||||
time: i64,
|
||||
base_time: i64,
|
||||
used_time: i64,
|
||||
count: i64,
|
||||
}
|
||||
|
||||
impl Ord for Worker {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
(other.used_time + other.time).cmp(&(self.used_time + self.time))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Worker {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec<i32>) -> i64 {
|
||||
let mut worker_times: BinaryHeap<Worker> = worker_times.iter().enumerate().map(|(i, v)| Worker {
|
||||
time: *v as i64,
|
||||
base_time: *v as i64,
|
||||
used_time: 0,
|
||||
count: 1,
|
||||
}).collect();
|
||||
|
||||
for _ in 0..mountain_height {
|
||||
let min_worker = worker_times.pop().unwrap();
|
||||
|
||||
worker_times.push(Worker {
|
||||
base_time: min_worker.base_time,
|
||||
count: min_worker.count + 1,
|
||||
used_time: min_worker.used_time + min_worker.time,
|
||||
time: min_worker.base_time * (min_worker.count + 1)
|
||||
});
|
||||
}
|
||||
|
||||
worker_times.iter().map(|w| w.used_time).max().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_416_2() {
|
||||
assert_eq!(10, Solution::min_number_of_seconds(5, vec![1, 5]));
|
||||
assert_eq!(3, Solution::min_number_of_seconds(4, vec![2,1,1]));
|
||||
assert_eq!(12, Solution::min_number_of_seconds(10, vec![3,2,2,4]));
|
||||
assert_eq!(15, Solution::min_number_of_seconds(5, vec![1]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user