
class Solution(object):
def numRescueBoats(self, people, limit):
"""
:type people: List[int]
:type limit: int
:rtype: int
"""
if people is None or len(people) == 0:
return 0
i = 0
j = len(people) - 1
res = 0
# 先排序很重要
people.sort()
while i <= j:
if people[i] + people[j] <= limit:
i += 1
j -= 1
res += 1
return res
该代码实现了一个名为`Solution`的类,其中的`numRescueBoats`方法用于计算在给定载重限制下,能承载一组人的最少船只数量。方法首先对人按体重进行排序,然后从两端开始配对,如果两个人的体重之和不超过船只载重限制,则可以一同乘坐一艘船。算法以这种策略减少了船只的使用数量。

被折叠的 条评论
为什么被折叠?



