File: minimum-number-of-moves-to-seat-everyone/solution.py

Date: 2026-06-06

Time: 17:59

minimum-number-of-moves-to-seat-everyone/solution.py

Purpose

This file solves LeetCode 2037: Minimum Number of Moves to Seat Everyone. It contains both the solution and its unit tests in a single module — the standard structure across this repository. The problem: given two arrays of equal length (seat positions and student positions), find the minimum total number of moves to assign each student to a unique seat, where a "move" is one position left or right.

Key Components

minmovesto_seat(seats, students) -> int — The sole function. It accepts two list[int] of equal length and returns the minimum total displacement. The entire implementation is a one-liner:


return sum(abs(a - b) for a, b in zip(sorted(seats), sorted(students)))

TestMinMovesToSeat — Seven test cases covering LeetCode's three examples, single-element input, already-matched arrays, uniform positions, and duplicate values.

Patterns

Sort-and-pair greedy. The solution sorts both arrays independently, then zips them together and sums the absolute differences. This is the canonical greedy approach for minimum-cost bipartite matching on a line: when both sequences are sorted, pairing the i-th smallest seat with the i-th smallest student is provably optimal. Any crossing assignment would increase total cost (by the rearrangement inequality).

Single-file solution+test layout. Consistent with every other problem directory in this repo — solution.py contains the function and inline unittest tests, runnable via python -m unittest or if _name == "main_".

Dependencies

Imports: Only unittest from the standard library. No external packages.

Imported by: The massive importedby list is misleading — it reflects test files across the repo that likely share a test runner (runtests.py) or import infrastructure, not direct usage of minmovesto_seat itself.

Flow

1. sorted(seats) and sorted(students) produce two ascending copies — O(n log n) each.

2. zip(...) pairs elements by index — the smallest seat with the smallest student, etc.

3. abs(a - b) computes per-pair displacement.

4. sum(...) aggregates total moves.

The entire computation is a single pass over two sorted arrays. Time complexity: O(n log n) dominated by the sorts. Space: O(n) for the sorted copies.

Invariants

Error Handling

None. The function trusts its inputs match the LeetCode specification. No length checks, no type validation. Empty lists return 0 (correct — sum of an empty generator).

Topics to Explore

Beliefs