class Solution { public: int numRescueBoats(vector<int>& people, int limit) { sort(people.begin(), people.end()); int n=people.size(); int ptri=0; int ptrl=n-1; int boats=0; for(ptrl=n-1;ptrl>=0&&ptrl>=ptri;ptrl--) { if(people[ptrl]+people[ptri]<=limit) ptri++; boats++; } return boats; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter