Skip to content

Commit

Permalink
Add problem 2037: Minimum Number of Moves to Seat Everyone
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 4, 2024
1 parent c58e6d4 commit a082f37
Show file tree
Hide file tree
Showing 3 changed files with 60 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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub struct Solution;

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

impl Solution {
pub fn min_moves_to_seat(seats: Vec<i32>, students: Vec<i32>) -> 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<i32>, students: Vec<i32>) -> i32 {
Self::min_moves_to_seat(seats, students)
}
}

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

pub trait Solution {
fn min_moves_to_seat(seats: Vec<i32>, students: Vec<i32>) -> i32;
}

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

pub fn run<S: Solution>() {
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);
}
}
}

0 comments on commit a082f37

Please sign in to comment.