20241024 finished.

This commit is contained in:
2024-10-24 09:08:13 +08:00
parent 3102da99a8
commit bce8de1c85
266 changed files with 2321 additions and 2014 deletions

View File

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

View File

@@ -3,24 +3,24 @@ 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
}
}
}

View File

@@ -13,7 +13,7 @@ struct Worker {
impl Ord for Worker {
fn cmp(&self, other: &Self) -> Ordering {
(other.used_time + other.time).cmp(&(self.used_time + self.time))
(other.used_time + other.time).cmp(&(self.used_time + self.time))
}
}
@@ -25,24 +25,28 @@ impl PartialOrd for Worker {
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();
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)
time: min_worker.base_time * (min_worker.count + 1),
});
}
worker_times.iter().map(|w| w.used_time).max().unwrap()
}
}
@@ -54,8 +58,8 @@ mod tests {
#[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!(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]));
}
}