Boats to Save People

Problem Id: 881 Difficulty: Medium


Intuition

Solution


class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        people.sort(reverse=True)
        i = 0
        j = len(people) - 1
        boats = 0

        while j > i:
            if people[i] + people[j] > limit:
                i += 1
                boats += 1
            else:
                i += 1
                j -= 1
                boats += 1
        if i == j:
            boats += 1
        return boats