Simpler || Faster || Fully explained
Intuition\n Describe your first thoughts on how to solve this problem. \nWe use two pointers, left and right to point from the lightest to the heaviest person a
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe use two pointers, left and right to point from the lightest to the heaviest person after sorting the array.\n\nif left + right value is less then limit, we can hold both of them in the same boat, so we move both the pointer to their next values, \ni.e., left++ and right--\n\nif left + right is > limit, then we can hold only the right person in the boat, so we move only the right pointer\n\nwe repeat the same process until left and right meets.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach used in this solution is to sort the array of people by their weights. Then, we use a two-pointer technique where the left pointer starts at the beginning of the array and the right pointer starts at the end of the array.\n\nWe initialize a variable boats to keep track of the number of boats required. We iterate through the array using the left and right pointers, moving them towards each other.\n\nAt each step, we check if the sum of the weights of the person at the left pointer and the person at the right pointer is less than or equal to the limit (the maximum weight a boat can carry). If it is, we increment the left pointer to move to the next person, as both persons can fit into the boat.\n\nIf the sum exceeds the limit, it means only the person at the right pointer can fit into the boat, so we decrement the right pointer.\n\nIn each iteration, we increment the boats counter to keep track of the number of boats required. We continue this process until the left pointer crosses the right pointer, indicating that we have considered all people in the array.\n\nFinally, we return the boats counter, which represents the minimum number of boats required to rescue all people.\n# Complexity\n- Time complexity:O(nlogn) Since we sort\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1) onlu two pointers are used\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nclass Solution {\n    public int numRescueBoats(int[] people, int limit) {\n        Arrays.sort(people);\n        int boats = 0;\n        int left = 0, right = people.length - 1;\n        while (left <= right) {\n            if (people[left] + people[right] <= limit) {\n                left++;\n            }\n            right--;\n            boats++;\n        }\n        return boats;\n    }\n}\n```\n```python []\nclass Solution(object):\n    def numRescueBoats(self, people, limit):\n        people.sort()\n        boats = 0\n        left, right = 0, len(people) - 1\n        while left <= right:\n            if people[left] + people[right] <= limit:\n                left += 1\n            right -= 1\n            boats += 1\n        return boats\n```\n```C# []\npublic class Solution {\n    public int NumRescueBoats(int[] people, int limit) {\n       Array.Sort(people);\n       int left = 0, right = people.Length-1;\n       int boats = 0;\n       while(left<=right){\n        if(people[left]+people[right]<=limit){\n            left++;\n        }\n        right--;\n        boats++;\n       }\n       return boats;\n    }\n}\n```
['Array', 'Two Pointers', 'Greedy', 'Sorting', 'Python', 'Java', 'Python3', 'C#']