Leetcode Note: Go - Minimum Number of Moves to Seat Everyone

Minimum Number of Moves to Seat Everyone - LeetCode
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/

  • Go 言語で取り組んだメモ

回答

[Go] Sort and calculate distance - Minimum Number of Moves to Seat Everyone - LeetCode
https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/solutions/1524878/go-sort-and-calculate-distance/

func minMovesToSeat(seats []int, students []int) int {
	sort.Ints(seats)
	sort.Ints(students)
	var diff int
	for i := range seats {
		diff += abs(seats[i] - students[i])
	}
	return diff
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}