1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| // @Title: 救生艇 (Boats to Save People)
// @Author: 15816537946@163.com
// @Date: 2021-08-26 11:25:11
// @Runtime: 88 ms
// @Memory: 7.5 MB
func numRescueBoats(people []int, limit int) int {
sort.Ints(people)
var ans int
left, right := 0, len(people)-1
// 注意题目给出的条件:1. 每个人体重不会超过 limit 2. 每艘船最多坐2个人
for left < right {
if people[left]+people[right] <= limit {
ans++
left++
right--
} else {
ans++
right--
}
}
if left == right {
ans++
}
return ans
}
|