EASY SOLUTION || DETAILED EXPLANATION || TWO POINTER || BEATS 100% OF USERS WITH C++|| O(1) SOLUTION
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem asks for the minimum number of boats needed to rescue all people, consideri
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem asks for the minimum number of boats needed to rescue all people, considering a weight limit per boat.\nThe code adopts a greedy approach: it tries to fit as many people as possible into each boat while adhering to the weight limit.\nSorting the people vector in ascending weight order allows us to efficiently pair the lightest and heaviest people in each iteration.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitialize two pointers i and j at the beginning and end of the sorted people vector, respectively. These pointers will iterate through potential pairs to fill a boat.\nUse a while loop to iterate as long as i (lightest person) is less than or equal to j (heaviest person).\nHeavyweight Exclusion (people[j] >= limit):\nIf the current heaviest person (people[j]) is too heavy to fit in any boat alone, exclude them by decrementing j.\nWeight Limit Exceeded (people[i] + people[i] > limit):\nIf the combined weight of the lightest and heaviest people (people[i] + people[j]) is greater than the limit, we cannot pair them. In this case, send the heavier person alone (j--) to make room for a lighter person in the next iteration.\nSuccessful Pairing:\nIf the combined weight is within the limit, it\'s a successful pairing. Send both people in the boat by incrementing i and decrementing j.\nIn each successful pairing or exclusion, increment the ans (boat count).\n\n# Complexity\n- Time complexity:\n     \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n logn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        int n = people.size();\n        int ans = 0;\n        sort(people.begin(), people.end());\n\n        if(people[0] >= limit)return n;\n\n        int i = 0, j = n-1;\n        while(i<=j){\n            if(people[j] >= limit )j--;\n            else if(people[j] < limit and people[i] + people[j] > limit)j--;\n            else i++, j--;\n            ans++;\n        }\n        return ans;\n    }\n};\n```
['Two Pointers', 'Greedy', 'Sorting', 'C++']