add: 416 week.

This commit is contained in:
jackfiled 2024-09-22 13:56:11 +08:00
parent 6605c2aca0
commit 5f58928e71
4 changed files with 90 additions and 0 deletions

View File

@ -2,4 +2,5 @@
pub mod util;
pub mod problem;
pub mod week_385;
pub mod week_416;
pub mod double_week_125;

2
src/week_416/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod problem1;
mod problem2;

26
src/week_416/problem1.rs Normal file
View 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
View 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]));
}
}