Minimum Number of Moves to Seat Everyone (LeetCode 2037): Solution and Explanation

Easy
Array
Greedy
Sorting
Counting Sort

n seats and n students sit at positions on a line. A move shifts one student by one position. Return the fewest moves that put every student in a different seat.

Intuition

Every student walks straight to a seat, so the cost of a plan is the sum of distances between each student and their seat. The only choice is the pairing.

Suppose two students' paths cross: the left student goes to a seat to the right of the seat the right student takes. Swapping their seats never makes the total longer, and it removes the crossing.

Keep uncrossing and you reach the pairing with no crossings at all: the leftmost student takes the leftmost seat, the second takes the second, and so on. Sorting both arrays produces exactly that pairing.

Approach: Sort both arrays, pair in order

  1. Sort seats and sort students.
  2. Set moves = 0.
  3. For each index i, add |seats[i] - students[i]| to moves.
  4. Return moves.

Step-by-step example

seats = [3,1,5], students = [2,7,4]
  1. Sorted seats: [1,3,5]. Sorted students: [2,4,7].
  2. Pair 0: student 2 to seat 1, 1 move. moves = 1.
  3. Pair 1: student 4 to seat 3, 1 move. moves = 2.
  4. Pair 2: student 7 to seat 5, 2 moves. moves = 4.
  5. Return 4.

Code: Python, Java, C++

1class Solution:
2    def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
3        seats.sort()
4        students.sort()
5        moves = 0
6        for seat, student in zip(seats, students):
7            moves += abs(seat - student)
8        return moves
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n)

Two sorts of n values dominate. The pairing pass is linear.

Space

O(1)

Only a counter besides the in-place sorts (which may use O(log n) stack depending on the language).

Other approaches

Counting sort (time O(n + P) with P = 100, space O(P))

Positions are between 1 and 100, so count seats and students per position and walk both count arrays with two pointers, pairing copies in order. Avoids comparison sorting.

Running balance (time O(n + P), space O(P))

Sweep positions left to right, keeping (students seen - seats seen). The absolute value of that balance is how many students must cross the gap to the next position, so summing it over gaps gives the same answer.

Common mistakes

  • Sending each student to the nearest seat. Two students can want the same seat, and a nearest-first choice can force someone far away later.
  • Sorting only one of the arrays. The pairing must be k-th smallest to k-th smallest on both sides.
  • Forgetting the absolute value. A student may move left or right.
  • Worrying about duplicate positions. Equal seats or equal students sort next to each other and the pairing still works.

Frequently asked questions

Why is the sorted pairing optimal?

If a pairing has two crossing paths, swapping their seats never increases the total distance on a line. The sorted pairing is the only one with no crossings, so nothing can beat it.

Do students get in each other's way?

No. The problem counts only moves, and several students may stand on the same position while walking.

Can the answer be large?

At most 100 students each walk at most 99 positions, so the total is under 10,000 and fits easily in an int.