mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
add: leetcode 2037 solution
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func minMovesToSeat(seats []int, students []int) int {
|
||||
sort.Ints(seats)
|
||||
sort.Ints(students)
|
||||
n := len(students)
|
||||
moves := 0
|
||||
for i := 0; i < n; i++ {
|
||||
moves += abs(seats[i], students[i])
|
||||
}
|
||||
return moves
|
||||
}
|
||||
|
||||
func abs(a, b int) int {
|
||||
if a > b {
|
||||
return a - b
|
||||
}
|
||||
return b - a
|
||||
}
|
||||
Reference in New Issue
Block a user