Two pointers approach simple explained!!!
Problem Description\n\nYou are given a list of weights of people and a limit representing the maximum weight a boat can carry. Each boat can carry at most two p
# Problem Description\n\nYou are given a list of weights of people and a limit representing the maximum weight a boat can carry. Each boat can carry at most two people at the same time, provided the sum of their weights does not exceed the limit. You need to find the minimum number of boats needed to rescue all people.\n\n# Approach\n\n1. **Sorting**: First, we sort the list of people by their weights. Sorting enables us to pair the heaviest person with the lightest person whenever possible, optimizing the number of boats required.\n  \n2. **Two-Pointer Technique**: We use a two-pointer technique to iterate through the sorted list from both ends towards the center. At each step, we consider the weights of the person at the start and end pointers.\n   - If the sum of weights is less than or equal to the limit, we increment the start pointer to move to the next person.\n   - Regardless of whether a person is paired or not, we always decrement the end pointer to move to the next person, as we can\'t leave anyone behind.\n\n3. **Counting Boats**: We count the number of boats used during the process. Each time we consider a pair of people (or a single person when no pairing is possible), we increment the boat count.\n\n4. **Termination**: We terminate the process when the start pointer crosses the end pointer, indicating that we have considered all people in the list.\n\n5. **Returning Result**: Finally, we return the total number of boats used.\n\n# Code\n\n```python\nclass Solution:\n    def numRescueBoats(self, people: List[int], limit: int) -> int:\n        people.sort()\n        start, end = 0, len(people) - 1\n        boats = 0\n        while start <= end:\n            weight = people[start] + people[end]\n            if weight <= limit:\n                start += 1\n            boats += 1\n            end -= 1\n        return boats\n```\n\n# Complexity Analysis\n\n- **Time Complexity**: \n  - The time complexity of this solution is O(n log n), where n is the number of people.\n  - This complexity arises from the sorting operation performed on the list of people, which takes O(n log n) time.\n  - The subsequent while loop iterates through the sorted list once, which takes O(n) time.\n\n- **Space Complexity**: \n  - The space complexity is O(1) since the algorithm uses only a constant amount of extra space regardless of the input size.\n  - The space usage doesn\'t grow with the size of the input array or any other input parameters. \n---\n> #### ***If You Found it helpful, please upvote it. Thanks***\n![WhatsApp Image 2024-04-30 at 11.36.31 AM (2).jpeg](https://assets.leetcode.com/users/images/2adcd1d9-cefc-457a-a816-365653ff8eca_1714541670.1824505.jpeg)
['Array', 'Two Pointers', 'Sorting', 'Python', 'Python3']