LeetCode 2037, Easy. Topics: Array, Greedy, Sorting, Counting Sort. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:
Problem
There are n availabe seats and n students standing in a room. You are given an array seats of length n, where seats[i] is the position of the i<sup>th</sup> seat. You are also given the array students of length n, where students[j] is the position of the j<sup>th</sup> student.
You may perform the following move any number of times:
- Increase or decrease the position of the
i<sup>th</sup> student by 1 (i.e., moving the i<sup>th</sup> student from position x to x + 1 or x - 1)
Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
Note that there may be multiple seats or students in the same position at the beginning.
Examples
Constraints
- n == seats.length == students.length
- 1 <= n <= 100
- 1 <= seats[i], students[j] <= 100
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026