Skip to main content

881. Boats to Save People

https://leetcode.com/problems/boats-to-save-people

Python

class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()

left, right = 0, len(people)-1
counter = 0

while left <= right:
if people[right] + people[left] <= limit:
right -= 1
left += 1
else:
right -= 1
counter += 1

return counter