[C++] Two Pointer, very fast method. (64ms)
I was trying a lot of variations on this, stuck at an O(n^2) but then it ticked that if we sort the list(as the order does\'nt matter.)\n\nAll you need to under
I was trying a lot of variations on this, stuck at an O(n^2) but then it ticked that if we sort the list(as the order does\'nt matter.)\n\nAll you need to understand is that, **The heaviest person only has the possibility to pair with the lightest one.** After that it all makes sense.\n\nJust initialise 2 pointers, and only move the left pointer forward if the heavier person can go with the lighter. i.e. `people[r] + people[l] <= limit`.\n\n**NOTE :** Although there is now a faster method by @Ajna as shown here  [soln](https://leetcode.com/problems/boats-to-save-people/discuss/1014797/)\n\n**CODE :**\n```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit)\n    {\n        sort(people.begin(), people.end());\n        int n = people.size();\n        int l = 0, r = n-1;\n        int ans = 0;\n        \n        while(l <= r)\n        {\n            if(people[r] + people[l] <= limit) l++;\n            \n            r--;\n            ans++;\n        }\n        \n        return ans;\n    }\n};\n```\n\n\n**RESULT :**\n\n<img src="https://assets.leetcode.com/users/images/e5b403d4-e121-44f1-87d7-018ccc87b832_1610526761.7596173.png" height=400 width=700>\n\n*Please Upvote if this helped. Feel free to ask for any clarifications.*
[]