Image Explanation🏆- [Brute -> Better -> Optimal] - C++/Java/Python
Video Solution (Aryan Mittal) - Link in LeetCode Profile\nBoats to Save People by Aryan Mittal\n\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n\n\n\n\n\n# O(nlogn) 
# Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Boats to Save People` by `Aryan Mittal`\n![meta6.png](https://assets.leetcode.com/users/images/ca06873f-5964-4145-b52d-2bd2a1aa91a4_1680487612.4583719.png)\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/0035e1e2-5a1e-4ae9-8abf-fe5ee13d6929_1680483975.115677.png)\n![image.png](https://assets.leetcode.com/users/images/d9e6b6dd-ffd4-4540-93d1-99195acfcc1f_1680483983.5888247.png)\n![image.png](https://assets.leetcode.com/users/images/46cce3ab-0573-4554-97f0-998bcc3751ad_1680483995.4880555.png)\n![image.png](https://assets.leetcode.com/users/images/fc160ead-d2fe-44bb-a3cb-6c66755cfc78_1680484005.319166.png)\n![image.png](https://assets.leetcode.com/users/images/fd38cbb5-39bf-4c34-a5e4-c7505c779682_1680484021.8231213.png)\n![image.png](https://assets.leetcode.com/users/images/50bd3305-febb-4f2e-9ef3-87189748f1da_1680484032.2835655.png)\n![image.png](https://assets.leetcode.com/users/images/52822661-6ff2-4567-8af3-9cf62444f514_1680484040.321599.png)\n![image.png](https://assets.leetcode.com/users/images/2a7ee61e-f9d1-4f00-abc8-0dd0ea85f76f_1680484049.6275468.png)\n![image.png](https://assets.leetcode.com/users/images/f75691a4-f4ba-4e47-a920-1b8739026be4_1680484058.411538.png)\n![image.png](https://assets.leetcode.com/users/images/6d8bc310-c2e4-4779-8fe9-40786120f726_1680484065.8801954.png)\n\n\n# O(nlogn) Code\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        sort(people.begin(), people.end());\n        \n        int boatCount = 0, left = 0, right = people.size() - 1;\n        while(left <= right){\n            if(people[left] + people[right] <= limit){\n                left++;\n                right--;\n            }\n            else{\n                right--;\n            }\n            boatCount++;\n        }\n        return boatCount;\n    }\n};\n```\n```Java []\nimport java.util.*;\n\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        Arrays.sort(people);\n        \n        int boatCount = 0, left = 0, right = people.length - 1;\n        while(left <= right){\n            if(people[left] + people[right] <= limit){\n                left++;\n                right--;\n            }\n            else{\n                right--;\n            }\n            boatCount++;\n        }\n        return boatCount;\n    }\n}\n```\n```Python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()\n        \n        boatCount, left, right = 0, 0, len(people) - 1\n        while left <= right:\n            if people[left] + people[right] <= limit:\n                left += 1\n                right -= 1\n            else:\n                right -= 1\n            boatCount += 1\n        return boatCount\n\n```\n\n# O(limit) Code\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        vector<int> count(limit+1, 0);\n        for(int p : people) count[p]++;\n\n        int idx = 0;\n        for(int val=1; val<=limit; val++){\n            while(count[val]-- > 0)\n                people[idx++] = val;\n        }\n        \n        int boatCount = 0, left = 0, right = people.size() - 1;\n        while(left <= right){\n            if(people[left] + people[right] <= limit){\n                left++;\n                right--;\n            }\n            else{\n                right--;\n            }\n            boatCount++;\n        }\n        return boatCount;\n    }\n};\n```\n```Java []\nimport java.util.*;\n\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int[] count = new int[limit+1];\n        for(int p : people) count[p]++;\n\n        int idx = 0;\n        for(int val=1; val<=limit; val++){\n            while(count[val]-- > 0)\n                people[idx++] = val;\n        }\n        \n        int boatCount = 0, left = 0, right = people.length - 1;\n        while(left <= right){\n            if(people[left] + people[right] <= limit){\n                left++;\n                right--;\n            }\n            else{\n                right--;\n            }\n            boatCount++;\n        }\n        return boatCount;\n    }\n}\n```\n```Python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        count = [0] * (limit+1)\n        for p in people:\n            count[p] += 1\n\n        idx = 0\n        for val in range(1, limit+1):\n            while count[val] > 0:\n                people[idx] = val\n                idx += 1\n                count[val] -= 1\n        \n        boatCount, left, right = 0, 0, len(people) - 1\n        while left <= right:\n            if people[left] + people[right] <= limit:\n                left += 1\n                right -= 1\n            else:\n                right -= 1\n            boatCount += 1\n        return boatCount\n```
['Two Pointers', 'Sorting', 'C++', 'Java', 'Python3']