Python | Easy
see the Successfully Accepted Submission\n\n# Code\n\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort(
[see the Successfully Accepted Submission](https://leetcode.com/problems/boats-to-save-people/submissions/1248787339/?envType=daily-question&envId=2024-05-04)\n\n# Code\n```\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()  # Sort the people by weight\n        left, right = 0, len(people) - 1  # Initialize two pointers\n        \n        boats = 0  # Initialize the number of boats\n        \n        while left <= right:\n            if people[left] + people[right] <= limit:\n                # If the sum of weights of left and right person is within the limit,\n                # both can go in the same boat\n                left += 1  # Move left pointer to the right\n                right -= 1  # Move right pointer to the left\n            else:\n                # If not, the heaviest person will go in a boat alone\n                right -= 1  # Move right pointer to the left\n            boats += 1  # Increment boat count for every iteration\n        \n        return boats\n\n```\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']