✅ Detailed Explanation🔥🔥Using Sorting and Two Pointer Approach🔥TC: O(nlogn), SC: O(1)🔥
\uD83C\uDFAFProblem Explaination\nWe are given list of people\'s weights as array people and asked to find minimum number of boats required to carry all of them
# \uD83C\uDFAFProblem Explaination\nWe are given list of people\'s weights as array `people` and asked to find minimum number of boats required to carry all of them at same time. Boat can carry 2 people at max and maximum weight it can carry is given as `limit`.\n\n# \uD83E\uDD14 Intuition\n- Each person\'s weight if less than limit. And at max we can send two people in a boat. \n- Let\'s try pairing heaviest person first. \n    - In case you can\'t find a pair, send the heaviest alone.\n    - In case you are able to find a person, pair with the lightest person and send them both.\n- Why you might ask? Good question, if someone is able to pair with heaviest, then can pair with any other person so its not a problem ever we send.\n- If were to sending the lightest person, then we can sort the `person` according to weights and check lightest and heaviest with Two Pointer approach.\n- We first chech if 1st lightest(`l`) and heaviest(`r`) can fit on boat, if they can pair them and send them in a boat,  else send only heaviest. Next choose lightest and heaviest left and continue pairing until all people are filled. \n\nThe intuition behind this solution is to sort the array of people\'s weights and then use a two-pointer approach to pair the heaviest person with the lightest person who can fit on the same boat, iterating inward until all people are paired or considered.\n\n\n# \uD83E\uDDE0 Approach\n1. Sort the array of people\'s weights in ascending order.\n2. Initialize two pointers, `l` and `r`, pointing to the start and end of the sorted array respectively.\n3. Initialize a variable `boats` to keep track of the number of boats required.\n4. Iterate through the array using the two-pointer approach:\n   - If the sum of weights at pointers `l` and `r` is less than or equal to the limit:\n     - Move both pointers inward (`l++` and `r--`).\n   - If the sum exceeds the limit:\n     - Move only the right pointer inward (`r--`).\n   - Increment the `boats` count after each iteration.\n5. Return the `boats` count, which represents the minimum number of boats required to rescue all people.\n\n# \uD83D\uDE97 Dry Run\nPeople: `[2, 3, 5, 2, 1, 4]`\nk: `5`\n| No.| People | l, r | Boat |\n|:---:|:---:|:---:|:---:|\n| 1 | [ 1, 2, 2, 3, 4, **5**] | _ , 5 | [ . , 5 ] |\n| 2 | [ **1**, 2, 2, 3, **4**, _ ] | 0, 4 | [ 1, 4 ] |\n| 3 | [ _, **2**, 2, **3**, _ , _ ] | 1, 3 | [ 2, 3 ] |\n| 4 | [ _ , _ , **2**, _ , _ , _ ] | 2, _ | [ 2, . ] |\n\n# \uD83D\uDCD2 Complexity\n- \u23F0 Time complexity: $$O(nlogn)$$\n  - Sorting the array takes O(nlogn) time.\n  - The two-pointer approach takes O(n) time.\n-  \uD83E\uDDFA Space complexity: $$O(1)$$\n  - Sorting the array in-place takes O(1) space.\n  - Additional variables (`l`, `r`, `boats`) take O(1) space.\n\n# \uD83E\uDDD1\u200D\uD83D\uDCBB Code\n``` python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        boats = 0\n        people.sort()\n        l, r = 0, len(people) - 1\n\n        while (l <= r):\n            if people[l] + people[r] <= limit:\n                l += 1\n                r -= 1\n            else:\n                r -= 1\n            boats += 1\n\n        return boats\n```\n``` Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        Arrays.sort(people);\n        int boats = 0;\n        int l = 0, r = people.length - 1;\n\n        while (l <= r) {\n            if (people[l] + people[r] <= limit) {\n                l++;\n                r--;\n            } else {\n                r--;\n            }\n            boats++;\n        }\n\n        return boats;\n    }\n}\n```\n``` cpp []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        sort(people.begin(), people.end());\n        int boats = 0;\n        int l = 0, r = people.size() - 1;\n\n        while (l <= r) {\n            if (people[l] + people[r] <= limit) {\n                l++;\n                r--;\n            } else {\n                r--;\n            }\n            boats++;\n        }\n\n        return boats;\n    }\n};\n```\n``` javascript []\nvar numRescueBoats = function(people, limit) {\n    people.sort((a, b) => a - b);\n    let boats = 0;\n    let l = 0, r = people.length - 1;\n\n    while (l <= r) {\n        if (people[l] + people[r] <= limit) {\n            l++;\n            r--;\n        } else {\n            r--;\n        }\n        boats++;\n    }\n\n    return boats;\n};\n```\n\n---\n## \uD83D\uDD28 Futher Optimising Code without using Helper Function\n##### Same logic\nIn both cases were sum of lightest(`l`) and heaviest(`r`) is `<=` or `>` we send heaviest(`r`). So let\'s just increment lightest `l++` only if sum is `<=` and always decrement heaviest `r--`.\n``` python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        boats = 0\n        people.sort()\n        l, r = 0, len(people) - 1\n\n        while (l <= r):\n            if people[l] + people[r] <= limit:\n                l += 1\n            r -= 1\n            boats += 1\n\n        return boats\n```\n``` Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        Arrays.sort(people);\n        int boats = 0;\n        int l = 0, r = people.length - 1;\n\n        while (l <= r) {\n            if (people[l] + people[r] <= limit) {\n                l++;\n            }\n            r--;\n            boats++;\n        }\n\n        return boats;\n    }\n}\n```\n``` cpp []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        sort(people.begin(), people.end());\n        int boats = 0;\n        int l = 0, r = people.size() - 1;\n\n        while (l <= r) {\n            if (people[l] + people[r] <= limit) {\n                l++;\n            }\n            r--;\n            boats++;\n        }\n\n        return boats;\n    }\n};\n```\n``` javascript []\nvar numRescueBoats = function(people, limit) {\n    people.sort((a, b) => a - b);\n    let boats = 0;\n    let l = 0, r = people.length - 1;\n\n    while (l <= r) {\n        if (people[l] + people[r] <= limit) {\n            l++;\n        }\n        r--;\n        boats++;\n    }\n\n    return boats;\n};\n```
['Two Pointers', 'Greedy', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']