Easy Python Solution, Beats 90%
Intuition\n Describe your first thoughts on how to solve this problem. \nThe easiest way to approach this problem, which I also believe is the fastest, is to do
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe easiest way to approach this problem, which I also believe is the fastest, is to do a two-pointer approach, where you take the sorted list, and move inwards, trying to cancel the lightest and heaviest people out. \n\nWhen moving from the heavier side, each time the right index moves to the left, it corresponds directly to an addition of a boat, this is because:\n    - 1. No complimentary weight was found, meaning the person will need their own boat, or\n    - 2. Complimentary weight(s) were found, and cancelled out with the index of the heavier side.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the list, by doing: ```people.sort()```\n2. Initialize the boat count ```boats```, right and left pointers, ```l``` and ```r```\n3. Loop through the sorted list, until the pointers cross, meaning all people have been accounted for: ```while l <= r:```\n4. Check if we can find complimentary pairs. Take the rightmost person ```person[r]``` and add the leftmost ```person[l]``` to see if they fall under the limit. If so, move both indices closer to each other. If no compliment exists, just move the right pointer 1 left.\n5. When finished, return boats.\n\n# NOTE!\n\nNow, this solution only works if at most **2** people are allowed on a boat at once! If this were not the case and any amount of people were allowed under the weight limit, it would require a loop instead of an if statement. I\'m honestly suprised they didn\'t do this (and wrote code to check for that initially), maybe it will come up as another medium question?\n\nPlease upvote if you found it helpful :)\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlogn) <- because of the ```people.sort()```\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()\n        boats = l = 0\n        r = len(people) - 1      \n        while l <= r:\n            if people[l] + people[r] <= limit:\n                l += 1\n            r, boats = r - 1, boats + 1\n        return boats\n```
['Two Pointers', 'Python', 'Python3']