C++✅ || 2 Pointer || Easy || Simple & Short || Algorithm
Intuition\nSorting the vector and using the two pointers approach.\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1- Sort the Ve
# Intuition\nSorting the vector and using the two pointers approach.\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1- Sort the Vector.\n2- We can make the lightest & heaviest person to go together.\nusing Two pointer appraoch i is the lightest person & j is the heaviest.\nTraversing till i <= j.\n3- If sum of lightest & heaviest person are under limit then both can go together , updating i & j both.\n4-If sum exceeds the limit, then heaviest person will go alone, hence updating only the j pointer.\nincrement the count .\n5- Return cnt.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09Please upvote if it helps \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\n\n# Code\n```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n         sort(people.begin() , people.end());\n\n        int i=0 , boats=0;\n        int j=people.size()-1;\n\n        while(i<=j){\n            if(people[i]+people[j]<=limit){\n                i++;\n                j--;\n                boats++;\n            }\n            else{\n                boats++;\n                j--;\n            }\n        }\n\n        return boats;\n    }\n};\n```
['Two Pointers', 'C++']