EASY || TWO POINTERS || BEGINNER-FRIENDLY
Intuition\n- We sort the people array first to optimize boat filling. \n- Then we iterate through the sorted array, trying to fit two people on each boat, start
# Intuition\n- We sort the people array first to optimize boat filling. \n- Then we iterate through the sorted array, trying to fit two people on each boat, starting with the lightest and heaviest. \n- If two people can fit on a boat, we increment our counter and move to the next pair. \n- If not, we move the heavier person to the next boat and continue.\n\n# Approach\n1. Sort the people array.\n2. Initialize variables i and j, representing the start and end indices of the array respectively.\n3. Use a while loop to iterate through the array.\n4. Check if the sum of weights of people at indices i and j is less than or equal to the limit.\n5. If yes, increment the count of boats and move both indices inward.\n6. If no, move only the j index inward.\n7. Return the total number of people minus the count of boats (as each boat can carry at most 2 people).\n\n# Complexity\n- Time complexity:\n **O(n log n)** due to sorting, where n is the number of people.\n\n- Space complexity:\n**O(1)**, as we are using constant extra space\n\n# Code\n```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        int n = people.size();\n        sort(people.begin(), people.end());\n        int two_count = 0;\n        int i = 0, j = n - 1;\n        while(i<j){\n            if(people[i] + people[j] <= limit){\n                two_count ++;\n                i++;\n                j--;\n            }else{\n                j--;\n            }\n        }\n        return n - two_count;// (n - 2 * two_count) + two_count\n    }\n};\n```
['Two Pointers', 'Sorting', 'Counting', 'C++']