go: add «2037. Minimum Number of Moves to Seat Everyone»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
fbc9fdb7ad
commit
d738179503
1 changed files with 23 additions and 0 deletions
23
go/minimum-number-of-moves-to-seat-everyone.go
Normal file
23
go/minimum-number-of-moves-to-seat-everyone.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
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
|
||||
}
|
Loading…
Reference in a new issue