To minimize the number of moves required to seat students, we aim to match each student to the closest available seat.
- Sort both the
seats
andstudents
slices to ensure they are in ascending order. - Iterate through both slices simultaneously.
- For each pair of corresponding elements (seat and student), calculate the absolute difference to determine the distance between the seat and the student.
- Accumulate the total moves by adding up these absolute differences.
- Return the total moves.
- Time complexity: O(n log n) due to sorting the
seats
andstudents
slices. - Space complexity: O(1) since we only use a constant amount of extra space for variables regardless of the input size.