g0801_0900.s0881_boats_to_save_people.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0801_0900.s0881_boats_to_save_people
// #Medium #Array #Sorting #Greedy #Two_Pointers
// #2023_04_08_Time_370_ms_(96.07%)_Space_44.8_MB_(99.70%)
class Solution {
fun numRescueBoats(people: IntArray, limit: Int): Int {
people.sort()
var i = 0
var j = people.size - 1
var boats = 0
while (i < j) {
if (people[i] + people[j] <= limit) {
boats++
i++
j--
} else if (people[i] + people[j] > limit) {
boats++
j--
}
}
return if (i == j) {
boats + 1
} else boats
}
}