🔥🔥 Easy to Understand Solution ❗❗ TC: O(n) ❗❗ Beats 100% 🔥🔥
Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n\n# Code\njava []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {
# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n\n# Code\n```java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        int[] buckets = new int[limit + 1];\n        int start = 0;\n        int end = buckets.length - 1;\n        int boats = 0;\n\n        for(int i : people ){\n            buckets[i]++;\n        } \n        \n        while(start <= end){\n            while(start <= end && buckets[start] <= 0 ) {\n                start++;\n            }\n            \n            while(start <= end && buckets[end] <= 0 ) {\n                end--;\n            }\n\n            if(buckets[start] <= 0 && buckets[end] <= 0) {\n                break;\n            }\n\n            boats++;\n            \n            if(start + end <= limit){\n                buckets[start]--;\n            }\n\n            buckets[end]--;\n        }\n\n        return boats;\n    }\n}\n```\n```python []\nclass Solution:\n    def numRescueBoats(self, people, limit):\n        buckets = [0] * (limit + 1)\n        start = 0\n        end = len(buckets) - 1\n        boats = 0\n\n        for weight in people:\n            buckets[weight] += 1\n        \n        while start <= end:\n            while start <= end and buckets[start] <= 0:\n                start += 1\n            \n            while start <= end and buckets[end] <= 0:\n                end -= 1\n\n            if buckets[start] <= 0 and buckets[end] <= 0:\n                break\n\n            boats += 1\n            \n            if start + end <= limit:\n                buckets[start] -= 1\n            \n            buckets[end] -= 1\n\n        return boats\n```\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        vector<int> buckets(limit + 1, 0);\n        int start = 0;\n        int end = buckets.size() - 1;\n        int boats = 0;\n\n        for (int weight : people) {\n            buckets[weight]++;\n        }\n\n        while (start <= end) {\n            while (start <= end && buckets[start] <= 0) {\n                start++;\n            }\n\n            while (start <= end && buckets[end] <= 0) {\n                end--;\n            }\n\n            if (buckets[start] <= 0 && buckets[end] <= 0) {\n                break;\n            }\n\n            boats++;\n\n            if (start + end <= limit) {\n                buckets[start]--;\n            }\n\n            buckets[end]--;\n        }\n\n        return boats;\n    }\n};\n```
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'Python', 'C++', 'Java', 'Python3']