✅✅Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand🔥
Please UPVOTE \uD83D\uDC4D\n\n!! BIG ANNOUNCEMENT !!\nI am currently Giving away my premium content well-structured assignments and study materials to clear int
# Please UPVOTE \uD83D\uDC4D\n\n**!! BIG ANNOUNCEMENT !!**\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies related to computer science and data science to my current Subscribers this week. I planned to give for next 10,000 Subscribers as well. So **DON\'T FORGET** to Subscribe\n\n**Search \uD83D\uDC49`Tech Wired leetcode` on YouTube to Subscribe**\n# OR \n**Click the Link in my Leetcode Profile to Subscribe**\n\nHappy Learning, Cheers Guys \uD83D\uDE0A\n\n# Video Solution\n**Search \uD83D\uDC49 `Boats to Save People by Tech Wired` on YouTube**\n\n![Yellow & Black Earn Money YouTube Thumbnail (9).png](https://assets.leetcode.com/users/images/fbc666a8-09cd-41c7-b526-f91e494ea186_1680485017.0872817.png)\n\n\n# Approach:\n\n- One possible approach to solving this problem is to sort the people by weight, and then use two pointers to traverse the sorted array from both ends. We can start with the heaviest person and the lightest person, and try to pair them up. If their combined weight is less than or equal to the boat\'s weight limit, we add them to a boat and move the lightest person\'s pointer to the next person. Otherwise, we only add the heaviest person to a boat.\n\n- We continue this process until all the people are either in a boat or have been paired up. The number of boats required is then the total number of iterations required to complete this process.\n\n# Intuition:\n\n- The intuition behind this approach is that we want to maximize the number of people we can put on a boat while ensuring that we don\'t exceed the weight limit of the boat. By starting with the heaviest and lightest person and pairing them up, we can ensure that we use the weight capacity of the boat as efficiently as possible. We then repeat this process with the next heaviest and lightest person, and so on, until we have either used all the people or run out of boats.\n\n- Sorting the people by weight is important because it allows us to pair up the heaviest and lightest person efficiently. If the people are not sorted, we might miss an optimal pairing and end up using more boats than necessary.\n\n- Overall, the time complexity of this approach is O(nlogn) due to the sorting step, where n is the number of people.\n\n```Python []\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()\n        i, j = 0, len(people) - 1\n        boats = 0\n        while i <= j:\n            if people[j] + people[i] <= limit:\n                i += 1\n            j -= 1\n            boats += 1\n        return boats\n\n```\n```Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        Arrays.sort(people);\n        int i = 0, j = people.length - 1;\n        int boats = 0;\n        while (i <= j) {\n            if (people[j] + people[i] <= limit) {\n                i++;\n            }\n            j--;\n            boats++;\n        }\n        return boats;\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        sort(people.begin(), people.end());\n        int i = 0, j = people.size() - 1;\n        int boats = 0;\n        while (i <= j) {\n            if (people[j] + people[i] <= limit) {\n                i++;\n            }\n            j--;\n            boats++;\n        }\n        return boats;\n    }\n};\n\n```\n![image.png](https://assets.leetcode.com/users/images/e2515d84-99cf-4499-80fb-fe458e1bbae2_1678932606.8004954.png)\n\n# Please UPVOTE \uD83D\uDC4D\n
['Two Pointers', 'Sorting', 'C++', 'Java', 'Python3']