Skip to content

Commit

Permalink
Add problem 2391: Minimum Amount of Time to Collect Garbage
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 11, 2025
1 parent 154f658 commit 51e7703
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@ pub mod problem_2385_amount_of_time_for_binary_tree_to_be_infected;
pub mod problem_2386_find_the_k_sum_of_an_array;
pub mod problem_2389_longest_subsequence_with_limited_sum;
pub mod problem_2390_removing_stars_from_a_string;
pub mod problem_2391_minimum_amount_of_time_to_collect_garbage;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::iter;

impl Solution {
pub fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32 {
let mut total_pickup_time = 0;
let mut total_metal_travel_time = 0;
let mut total_plastic_travel_time = 0;
let mut total_glass_travel_time = 0;
let mut total_travel_time = 0;

garbage
.iter()
.zip(iter::once(0).chain(travel))
.for_each(|(garbage, travel_cost)| {
total_pickup_time += garbage.len() as i32;

let mut state = 0;

for c in garbage.bytes() {
state |= match c {
b'M' => 1,
b'P' => 2,
_ => 4,
};

if state == 7 {
break;
}
}

total_travel_time += travel_cost;

if state & 1 != 0 {
total_metal_travel_time = total_travel_time;
}

if state & 2 != 0 {
total_plastic_travel_time = total_travel_time;
}

if state & 4 != 0 {
total_glass_travel_time = total_travel_time;
}
});

total_pickup_time + total_metal_travel_time + total_plastic_travel_time + total_glass_travel_time
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32 {
Self::garbage_collection(garbage, travel)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
24 changes: 24 additions & 0 deletions src/problem_2391_minimum_amount_of_time_to_collect_garbage/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pub mod iterative;

pub trait Solution {
fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
((&["G", "P", "GP", "GG"] as &[_], &[2, 4, 3] as &[_]), 21),
((&["MMM", "PGM", "GP"], &[3, 10]), 37),
];

for ((garbage, travel), expected) in test_cases {
assert_eq!(
S::garbage_collection(garbage.iter().copied().map(str::to_string).collect(), travel.to_vec()),
expected,
);
}
}
}

0 comments on commit 51e7703

Please sign in to comment.