Simple Solution with Diagrams in Video - C++, Java, Python, JS
Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtu
# Video\nPlease upvote here so others save time too!\n\nLike the video on YouTube if you found it useful\n\nClick here to subscribe on YouTube:\nhttps://www.youtube.com/@mayowadan?sub_confirmation=1\n\nhttps://youtu.be/p3AXumcpW98\n\n\n```javascript []\n/**\n * @param {number[]} people\n * @param {number} limit\n * @return {number}\n */\nvar numRescueBoats = function (people, limit) {\n    // Sort the list of people in ascending order of\n    // weight\n    people = people.sort(function (a, b) {\n        return a - b;\n    });\n    // Initialize pointers for the lightest and heaviest\n    // person\n    let left = 0;\n    let right = people.length - 1;\n\n    // Initialize the number of boats needed\n    let boats = 0;\n\n    // Loop through the list of people until all people\n    // have been rescued\n    while (left <= right) {\n        // Check if the lightest and heaviest person \n        // on the same boat can fit\n        if (people[left] + people[right] <= limit) {\n            // If they can, move on to the next lightest\n            // person\n            left += 1;\n        }\n        // Move on to the next heaviest person\n        right -= 1;\n\n        // Add a boat for the current group of people\n        boats += 1;\n    }\n    // Return the total number of boats needed\n    return boats;\n};\n```\n```python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        # Sort the list of people in ascending order of weight\n        people.sort()\n        \n        # Initialize pointers for the lightest and heaviest person\n        left = 0\n        right = len(people) - 1\n\n        # Initialize the number of boats needed\n        boats = 0\n\n        # Loop through the list of people until \n        # all people have been rescued\n        while left <= right:\n            # Check if the lightest and heaviest person on \n            # the same boat can fit\n            if people[left] + people[right] <= limit:\n                # If they can, move on to the next lightest person\n                left += 1\n            # Move on to the next heaviest person\n            right -= 1\n\n            # Add a boat for the current group of people\n            boats += 1\n        # Return the total number of boats needed\n        return boats\n\n```\n```Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        // Sort the list of people in ascending order of weight\n        Arrays.sort(people);\n        // Initialize pointers for the lightest and heaviest person\n        int left = 0;\n        int right = people.length - 1;\n\n        // Initialize the number of boats needed\n        int boats = 0;\n\n        // Loop through the list of people until all people have been rescued\n        while (left <= right) {\n            // Check if the lightest and heaviest person on the same boat can fit\n            if (people[left] + people[right] <= limit) {\n                // If they can, move on to the next lightest person\n                left++;\n            }\n            // Move on to the next heaviest person\n            right--;\n\n            // Add a boat for the current group of people\n            boats++;\n        }\n        // Return the total number of boats needed\n        return boats;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        // Sort the list of people in ascending order of weight\n        sort(people.begin(), people.end());\n        \n        // Initialize pointers for the lightest and heaviest person\n        int left = 0;\n        int right = people.size() - 1;\n\n        // Initialize the number of boats needed\n        int boats = 0;\n\n        // Loop through the list of people until all people have been rescued\n        while (left <= right) {\n            // Check if the lightest and heaviest person on the same boat can\n            // fit\n            if (people[left] + people[right] <= limit) {\n                // If they can, move on to the next lightest person\n                left++;\n            }\n            // Move on to the next heaviest person\n            right--;\n\n            // Add a boat for the current group of people\n            boats++;\n        }\n        // Return the total number of boats needed\n        return boats;\n    }\n};\n```\n
['Python', 'C++', 'Java', 'Python3', 'JavaScript']