Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

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 列表