-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2037: Minimum Number of Moves to Seat Everyone
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/problem_2037_minimum_number_of_moves_to_seat_everyone/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
src/problem_2037_minimum_number_of_moves_to_seat_everyone/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |