881. 救生艇
题目描述
第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit。
每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit。
返回载到每一个人所需的最小船数。(保证每个人都能被船载)。
原题请参考链接https://leetcode-cn.com/problems/boats-to-save-people/
题解
方法一 【双指针】
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
l = len(people)
start = 0
end = l - 1
s = 0
while start <= end:
if people[start] + people[end] <= limit:
s += 1
start += 1
end -= 1
else:
s += 1
end -= 1
return s
python

浙公网安备 33010602011771号