🌟☃️ Beats 97% | Space O(1) | Time O(nlogn) | Python3 | Medium ✅
Submission\n\n\n\n\n# Intuition\n\n- From the question, we got to know the boat\'s max capacity is limit and the boat can carry at most 2 people at once, so if 
# Submission\n\n![boats_to_save_people.png](https://assets.leetcode.com/users/images/6739b4d5-398e-44a8-8098-21df6c11017f_1714802471.1987972.png)\n\n\n# Intuition\n\n- From the question, we got to know the boat\'s max capacity is `limit` and the boat can carry at most 2 people at once, so if the heaviest person comes to the boat, we should find a corresponding match so it\'s the lightest person. Accordingly, we must match 1st heaviest to `1st lightest, 2nd heaviest to 2nd lightest, and so on`.\n\n- If the sum of the heaviest guy and the lightest guy is more than the limit, then we pick only the heavy guy, so `one in a boat if thats the case`, so you have got the idea to tackle it. The first step is that we need to `sort the array` and perform `two pointers` in order to solve.\n\n\n# Approach\n\n1. Sort the given `people` list, array, or vector.\n```\npeople.sort()\n```\n\n2. Take a variable i, which will be our leftmost pointer, and j, which will be our rightmost pointer, and a boat variable; the minimum `boat required will be assigned to zero`.\xA0\n\n```\ni = 0\nj = len(people)-1\nboat = 0\n```\n\n3. You must run the loop until i (the left pointer) is less than or equal to j (the right pointer).\n\n```\nwhile(i<=j):\n```\n\n4. Check if the sum of the values pointed by the left pointer and the right pointer is less than or equal to the limit; if yes, then `increment i by 1 and decrement j by 1`; if not, then just `decrement j by 1.`\n\n```\nif people[j]+people[i] <= limit:\n    i+=1\nj-=1\n```\n\n5. In each iteration until the loop terminates, we need to increment the minimum number of boats required, `which is the boat variable, by 1 for each iteration`.\n\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n\n\n- Space complexity: $$O(1)$$\n\n\n# Code\n```\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()\n\n        i = 0\n        j = len(people)-1\n        boat = 0\n\n        while(i<=j):\n            if people[j]+people[i] <= limit:\n                i+=1\n            j-=1\n            boat+=1\n\n        return boat\n```
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'Python3']