206. Minimum Number of Moves to Seat Everyone

EasyStringString

There are n seats and n students in a room. You are given arrays seats and students, both of length n.

A move consists of increasing or decreasing a student's position by 1.

Return the minimum number of moves to move each student to a seat such that no two students share the same seat.

Input format: seats values '|' separated, comma, then students values '|' separated.

Input: Two groups separated by ',': seats (values '|' separated) and students (values '|' separated).

Output: An integer — the minimum total moves.

Examples

Example 1
Input: 3|1|5,2|7|4
Output: 4
Explanation: Sort both: seats=[1,3,5], students=[2,4,7]. |1-2|+|3-4|+|5-7|=1+1+2=4.
Example 2
Input: 4|1|5|9,1|3|2|6
Output: 7
Explanation: Sort seats=[1,4,5,9], students=[1,2,3,6]. |0|+|2|+|2|+|3|=7.
Example 3
Input: 2|2|6|6,1|3|2|6
Output: 4
Explanation: Optimal assignment gives total 4.

Constraints

Asked by

AmazonGoogleMeta
Solve this problem in the editor →