✔️✔️Easy Solutions in Java ✔️✔️, Python ✔️, and C++ ✔️🧐Look at once 💻 with Exaplanation
Intuition\n Describe your first thoughts on how to solve this problem. \nThe basic idea is to sort the array in non-decreasing order, then use two pointers to r
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe basic idea is to sort the array in non-decreasing order, then use two pointers to represent the lightest and heaviest people, and gradually move them towards each other while keeping track of the number of boats used. Since we want to minimize the number of boats used, we want to put as many people on each boat as possible, subject to the weight limit.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the `people` array in non-decreasing order.\n\n2. Initialize the left pointer to the first element of the array, and the right pointer to the last element of the array.\n\n3. Initialize a counter variable `boats` to 0.\n\n4. While the left pointer is less than or equal to the right pointer, do the following:\na. Check if the sum of the weights of the people at the left and right pointers is less than\nor equal to the `limit`.\nb. If the sum is less than or equal to the `limit`, move the left pointer one step to the\nright to include the next lightest person.\nc. Move the right pointer one step to the left to include the next heaviest person,\nregardless of whether or not they can fit on the current boat.\nd. Increment the `boats` counter by 1.\n\n5. Return the `boats` counter, which represents the total number of boats used.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this approach is $$O(n log n)$$ , where n is the length of the `people` array. This is because we use the `sort` function to sort the array in nondecreasing order, which has a time complexity of $$O(n log n)$$, and we use two pointers to traverse the sorted array, which has a time complexity of $$O(n)$$.\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this approach is $$O(1)$$, because we only use a\nconstant amount of extra space to store the left and right pointers and the `boats` counter.\n\n![image.png](https://assets.leetcode.com/users/images/b427e686-2e5d-469a-8e7a-db5140022a6b_1677715904.0948765.png)\n\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n```\n\n# Code\n``` Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        Arrays.sort(people);\n        int left = 0, right = people.length - 1, boats = 0;\n        while (left <= right) {\n            if (people[left] + people[right] <= limit) {\n                left++;\n            }\n            right--;\n            boats++;\n        }\n        return boats;\n    }\n}\n\n```\n```javascript []\n/**\n * @param {number[]} people\n * @param {number} limit\n * @return {number}\n */\nvar numRescueBoats = function(people, limit) {\n    people.sort(function(a, b) { return a - b; });\n    var left = 0, right = people.length - 1, boats = 0;\n    while (left <= right) {\n        if (people[left] + people[right] <= limit) {\n            left++;\n        }\n        right--;\n        boats++;\n    }\n    return boats;\n};\n\n```\n```python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()\n        left, right, boats = 0, len(people) - 1, 0\n        while left <= right:\n            if people[left] + people[right] <= limit:\n                left += 1\n            right -= 1\n            boats += 1\n        return boats\n\n```\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        sort(people.begin(), people.end());\n        int left = 0, right = people.size() - 1, boats = 0;\n        while (left <= right) {\n            if (people[left] + people[right] <= limit) {\n                left++;\n            }\n            right--;\n            boats++;\n        }\n        return boats;\n    }\n};\n\n```\n# Please Comment\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution comment below if you like it.\uD83D\uDE0A\n```
['Two Pointers', 'Python', 'C++', 'Java', 'JavaScript']