Ctrl K

Scooters to Chargers

Easy
There are n charging stations and n e-scooters along a street. chargers[i] is the position of station i and scooters[j] is the position of scooter j. Several stations or scooters may share a position. Every scooter must be rolled to a station so that each station ends up with exactly one scooter. Moving a scooter by 1 in either direction costs 1. Return the smallest total cost.
Example 1
chargers=[2, 8, 5], scooters=[9, 1, 4] → 3

The scooter at 1 goes to 2, the one at 4 to 5 and the one at 9 to 8: 1 + 1 + 1 = 3.

Example 2
chargers=[3, 3], scooters=[1, 6] → 5

Both chargers sit at the same point: 2 + 3 = 5.

Constraints
  • n == len(chargers) == len(scooters)
  • 1 ≤ n ≤ 100
  • 1 ≤ chargers[i], scooters[j] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStrength of the Weaker Partner EasyMost Snacks on a Budget MediumOne Rook per Line MediumCake Slices for Guests EasyTriangle From Sticks Easy
loading Python…
Input
chargers=[2, 8, 5], scooters=[9, 1, 4]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 98 tests on the server, hidden ones included.