diff --git a/src/lib.rs b/src/lib.rs index 44e2e544..4e6cd12c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1403,6 +1403,7 @@ pub mod problem_2022_convert_1d_array_into_2d_array; pub mod problem_2027_minimum_moves_to_convert_string; pub mod problem_2028_find_missing_observations; pub mod problem_2032_two_out_of_three; +pub mod problem_2037_minimum_number_of_moves_to_seat_everyone; pub mod problem_2038_remove_colored_pieces_if_both_neighbors_are_the_same_color; pub mod problem_2042_check_if_numbers_are_ascending_in_a_sentence; pub mod problem_2043_simple_bank_system; diff --git a/src/problem_2037_minimum_number_of_moves_to_seat_everyone/iterative.rs b/src/problem_2037_minimum_number_of_moves_to_seat_everyone/iterative.rs new file mode 100644 index 00000000..196a5a72 --- /dev/null +++ b/src/problem_2037_minimum_number_of_moves_to_seat_everyone/iterative.rs @@ -0,0 +1,37 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn min_moves_to_seat(seats: Vec, students: Vec) -> i32 { + let mut seats = seats; + let mut students = students; + + seats.sort_unstable(); + students.sort_unstable(); + + let mut result = 0; + + for (seat, student) in seats.into_iter().zip(students) { + result += seat.abs_diff(student); + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn min_moves_to_seat(seats: Vec, students: Vec) -> i32 { + Self::min_moves_to_seat(seats, students) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2037_minimum_number_of_moves_to_seat_everyone/mod.rs b/src/problem_2037_minimum_number_of_moves_to_seat_everyone/mod.rs new file mode 100644 index 00000000..a3d239c3 --- /dev/null +++ b/src/problem_2037_minimum_number_of_moves_to_seat_everyone/mod.rs @@ -0,0 +1,22 @@ +pub mod iterative; + +pub trait Solution { + fn min_moves_to_seat(seats: Vec, students: Vec) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((&[3, 1, 5] as &[_], &[2, 7, 4] as &[_]), 4), + ((&[4, 1, 5, 9], &[1, 3, 2, 6]), 7), + ((&[2, 2, 6, 6], &[1, 3, 2, 6]), 4), + ]; + + for ((seats, students), expected) in test_cases { + assert_eq!(S::min_moves_to_seat(seats.to_vec(), students.to_vec()), expected); + } + } +}