mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-09-20 01:56:57 +02:00
24 lines
390 B
Go
24 lines
390 B
Go
|
package minimum_number_of_moves_to_seat_everyone
|
||
|
|
||
|
import (
|
||
|
"slices"
|
||
|
)
|
||
|
|
||
|
func minMovesToSeat(seats []int, students []int) int {
|
||
|
abs := func(value int) int {
|
||
|
return max(-value, value)
|
||
|
}
|
||
|
|
||
|
// sort them to get minimal distances
|
||
|
slices.Sort(seats)
|
||
|
slices.Sort(students)
|
||
|
|
||
|
moves := 0
|
||
|
for i, seat := range seats {
|
||
|
student := students[i]
|
||
|
moves += abs(seat - student)
|
||
|
}
|
||
|
|
||
|
return moves
|
||
|
}
|