Easiest C++/Java/Python3 Code (HIGHLY UNDERSTANDABLE) (INTUITIVE EXPLANATION)
Intuition\n> The algorithm pairs lightest and heaviest people within boat limits, minimizing boats needed by efficient space usage, facilitated by sorting weigh
# Intuition\n> The algorithm pairs lightest and heaviest people within boat limits, minimizing boats needed by efficient space usage, facilitated by sorting weights beforehand.\n\n# Approach\n\n1. **Sort the Array**: Begin by sorting the array of people\'s weights. Sorting simplifies the process of allocating boats.\n\n2. **Initialize Pointers**: Create two pointers, one pointing to the lightest person (start of the array) and the other to the heaviest person (end of the array).\n\n3. **Boat Allocation Loop**: While the pointer to the lightest person is less than or equal to the pointer to the heaviest person, do the following:\n\n   - If the combined weight of the lightest and heaviest persons does not exceed the boat limit, allocate a boat for them. Move both pointers towards each other.\n   \n   - If only the heaviest person\'s weight does not exceed the boat limit, allocate a boat for them and move the pointer to the heaviest person.\n\n4. **Count Boats**: Keep track of the number of boats allocated during the process.\n\n5. **Return Count**: Finally, return the total count of boats required to rescue all the people.\n\n# Complexity\n- Time complexity: O(nlogn) + O(n) = O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        // Sort the array to simplify the boat allocation process\n        sort(people.begin(), people.end());\n\n        // Initialize two pointers, one at the beginning and one at the end of the array\n        int left = 0; // Pointer for the lightest person\n        int right = people.size() - 1; // Pointer for the heaviest person\n        int boatsCount = 0; // Count of boats required\n\n        // Iterate until both pointers meet or cross each other\n        while (left <= right) {\n            // If both the lightest and heaviest persons can fit in the same boat\n            if (people[left] + people[right] <= limit) {\n                boatsCount++; // Allocate a boat\n                left++; // Move to the next lightest person\n                right--; // Move to the next heaviest person\n            } \n            // If only the heaviest person can fit in a boat\n            else if (people[right] <= limit) {\n                boatsCount++; // Allocate a boat\n                right--; // Move to the next heaviest person\n            }\n        }\n\n        return boatsCount; // Return the total count of boats required\n    }\n};\n```\n```Python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        # Sort the array to simplify the boat allocation process\n        people.sort()\n\n        # Initialize two pointers, one at the beginning and one at the end of the array\n        left = 0  # Pointer for the lightest person\n        right = len(people) - 1  # Pointer for the heaviest person\n        boats_count = 0  # Count of boats required\n\n        # Iterate until both pointers meet or cross each other\n        while left <= right:\n            # If both the lightest and heaviest persons can fit in the same boat\n            if people[left] + people[right] <= limit:\n                boats_count += 1  # Allocate a boat\n                left += 1  # Move to the next lightest person\n                right -= 1  # Move to the next heaviest person\n            # If only the heaviest person can fit in a boat\n            elif people[right] <= limit:\n                boats_count += 1  # Allocate a boat\n                right -= 1  # Move to the next heaviest person\n\n        return boats_count  # Return the total count of boats required\n\n```\n```Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        // Sort the array to simplify the boat allocation process\n        Arrays.sort(people);\n\n        // Initialize two pointers, one at the beginning and one at the end of the array\n        int left = 0; // Pointer for the lightest person\n        int right = people.length - 1; // Pointer for the heaviest person\n        int boatsCount = 0; // Count of boats required\n\n        // Iterate until both pointers meet or cross each other\n        while (left <= right) {\n            // If both the lightest and heaviest persons can fit in the same boat\n            if (people[left] + people[right] <= limit) {\n                boatsCount++; // Allocate a boat\n                left++; // Move to the next lightest person\n                right--; // Move to the next heaviest person\n            } \n            // If only the heaviest person can fit in a boat\n            else if (people[right] <= limit) {\n                boatsCount++; // Allocate a boat\n                right--; // Move to the next heaviest person\n            }\n        }\n\n        return boatsCount; // Return the total count of boats required\n    }\n}\n\n```\n
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'C++', 'Java', 'Python3']