Shortest Walk to Seats
seats and students list positions on a number line and have the same length. Every student must end up in a different seat, one student per seat, and a student who walks from position p to a seat at position q walks |p - q| units. Positions may repeat in either list.
Return the smallest possible total distance walked by all students. The smallest total is the same for every assignment that achieves it, so no tie rule is needed. Return 0 when both lists are empty.
Example 1
Seats 1, 3, 5 and students 2, 4, 7 in sorted order: the students walk 1, 1 and 2 units.
Example 2
The student at 1 keeps the seat at 1; the others walk 2, 2 and 3 units.
Constraints
0 <= len(seats) <= 100len(students) == len(seats)-1000 <= seats[i], students[i] <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Greedy problems
- Stepping StonesBeginner
- Third One FreeBeginner
- Two Numbers From Four DigitsBeginner
- Array PartitionEasy
- Assign CookiesEasy
- Best Haul With Partial SacksEasy
- Best Time to Trade Stock Many TimesEasy
- Can Place FlowersEasy