✍ ✅ [JavaScript][PHP] Two-Pointer Approach to Solving the Rescue Boat Problem
Approach\nThis approach uses a greedy algorithm to solve the problem. \nIt sorts the array of passengers by weight in ascending order.\nA left pointer is initia
# Approach\nThis approach uses a greedy algorithm to solve the problem. \nIt **sorts the array** of passengers by weight in ascending order.\nA **left pointer is** initialized to the **first element** of the array and a **right pointer is** initialized to the **last element** of the array(heaviest passenger).\n\nThe elements at the left pointer(**lighter passenger**) **and** right pointer(**heaviest passenger**) are added together and the total is compared to the limit. If the total **is greater than the limit**, **only** the element at the right pointer(**heaviest passenger**) **is taken** to the boat and the *right pointer is decremented*. \n\nIf the **total is less** than or equal to the limit, **both passengers are taken** and the *left pointer is incremented*. \n\nOn each iteration **increment the number of boats needed**. \n\nThis **continues until the left pointer is greater than the right** pointer(until all passengers are assigned to boats), at which point all elements have been processed and the *total number of boats is returned*. \n\n\n# Complexity\n*Time Complexity*: O(n log n) due to the sorting of the array.\n*Space Complexity*: O(1) as no additional space is used.\n\n\n\n```php []\nclass Solution {\n    /**\n     * @param Integer[] $people\n     * @param Integer $limit\n     * @return Integer\n     */\n    function numRescueBoats(array $people, int $limit): int {\n        // the idea is to collect the maximum sum to limit for each boat\n        // two poiners approach\n        // sort the array\n        sort($people);\n        // set the pointers\n        $leftPointer = 0;\n        $rightPointer = count($people) - 1;\n        // set the counter\n        $boatsCounter = 0;\n        // loop until the pointers meet\n        while ($leftPointer <= $rightPointer) {\n            // we can always put the heaviest person in a boat\n            // also we can put up to two people in a boat\n            // So, if the sum of the two people is less than the limit\n            if ($people[$leftPointer] + $people[$rightPointer] <= $limit) {\n                // increment the left pointer\n                $leftPointer++;\n            }\n            // decrement the right pointer\n            $rightPointer--;\n            // increment the boats counter\n            $boatsCounter++;\n        }\n        // return the boats counter\n        return $boatsCounter;\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    let boatsCount = 0;\n    people.sort((a, b) => a - b);\n\n    let leftPointer = 0;\n    let rightPointer = people.length - 1;\n\n    while (leftPointer <= rightPointer) {\n        if ((people[rightPointer] + people[leftPointer]) <= limit) {\n            leftPointer++;\n        }\n        rightPointer--;\n        boatsCount++;\n    }\n    return boatsCount;\n};\n```\n\n##### Thanks for reading! If you have any questions or suggestions, please leave a comment below. I would love to hear your thoughts! \uD83D\uDE0A\n\n### **Please upvote if you found this post helpful! \uD83D\uDC4D**\n\n\n\n\n\n\n
['Array', 'Two Pointers', 'PHP', 'JavaScript']