mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-24 19:04:32 +08:00
25 lines
333 B
Go
25 lines
333 B
Go
package leetcode
|
|
|
|
import (
|
|
"sort"
|
|
)
|
|
|
|
func numRescueBoats(people []int, limit int) int {
|
|
sort.Ints(people)
|
|
left, right, res := 0, len(people)-1, 0
|
|
for left <= right {
|
|
if left == right {
|
|
res++
|
|
return res
|
|
}
|
|
if people[left]+people[right] <= limit {
|
|
left++
|
|
right--
|
|
} else {
|
|
right--
|
|
}
|
|
res++
|
|
}
|
|
return res
|
|
}
|