From 5f58928e71a686374ce4206b0d74b5029428f8cc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 22 Sep 2024 13:56:11 +0800 Subject: [PATCH] add: 416 week. --- src/lib.rs | 1 + src/week_416/mod.rs | 2 ++ src/week_416/problem1.rs | 26 +++++++++++++++++ src/week_416/problem2.rs | 61 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/week_416/mod.rs create mode 100644 src/week_416/problem1.rs create mode 100644 src/week_416/problem2.rs diff --git a/src/lib.rs b/src/lib.rs index eb9b5e5..1f9bc53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,5 @@ pub mod util; pub mod problem; pub mod week_385; +pub mod week_416; pub mod double_week_125; diff --git a/src/week_416/mod.rs b/src/week_416/mod.rs new file mode 100644 index 0000000..5de73d0 --- /dev/null +++ b/src/week_416/mod.rs @@ -0,0 +1,2 @@ +mod problem1; +mod problem2; \ No newline at end of file diff --git a/src/week_416/problem1.rs b/src/week_416/problem1.rs new file mode 100644 index 0000000..396fc52 --- /dev/null +++ b/src/week_416/problem1.rs @@ -0,0 +1,26 @@ +pub struct Solution; + +impl Solution { + pub fn report_spam(message: Vec, banned_words: Vec) -> 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 + } +} \ No newline at end of file diff --git a/src/week_416/problem2.rs b/src/week_416/problem2.rs new file mode 100644 index 0000000..8694a0b --- /dev/null +++ b/src/week_416/problem2.rs @@ -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 { + Some(self.cmp(other)) + } +} + +impl Solution { + pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec) -> i64 { + let mut worker_times: BinaryHeap = 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])); + } +}