Problem statement

https://binarysearch.com/problems/Rocketship-Rescue/

Solution

Equal to Leetcode 0881. Boats to Save People.

Complexity

It is O(n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, people, limit):
        people.sort()
        beg, end, ans = 0, len(people) - 1, 0
        while beg <= end:
            if people[beg] + people[end] <= limit:
                beg += 1
            ans += 1
            end -= 1
                
        return ans