Kotlin Leetcode - 2037. Minimum Number of Moves to Seat Everyone
class Solution {
fun minMovesToSeat(seats: IntArray, students: IntArray): Int {
}
}
解題思路
這一題考的是對陣列的處理
Kotlin 參考解答
參考解法如下
class Solution {
fun minMovesToSeat(seats: IntArray, students: IntArray): Int {
val seatsArray = seats.sorted()
val studentsArray = students.sorted()
var maxValue = 0
for (i in seats.indices) {
maxValue += Math.abs(seatsArray[i] - studentsArray[i])
}
return maxValue
}
}
回到 leetcode 列表