✅ Detailed Approach🔥🔥Using Sorting and Two Pointer Approach🔥TC: O(nlogn), SC: O(1)🔥
Approach\n Describe your approach to solving the problem. \n\nThe approach in this code can be summarized as follows:\n\nSorting: Initially, the weights of peop
# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe approach in this code can be summarized as follows:\n\n**Sorting**: Initially, the weights of people are sorted in non-decreasing order. Sorting helps in efficiently pairing people while minimizing the number of boats required.\n\n**Greedy Pairing**: Using a greedy strategy, the code pairs the lightest person with the heaviest person for each boat. This approach optimizes boat usage by accommodating as many people as possible within the weight limit of each boat.\n\n**Two Pointers Technique**: The solution utilizes two pointers (i and j) initialized at the start and end of the sorted array, respectively. These pointers move inward, allowing the code to efficiently pair people for each boat.\n\n**Boat Counting**: For each boat, if the combined weight of the people at pointers i and j does not exceed the limit, both pointers move inward to form a boat. If the weight limit is exceeded, only pointer j moves inward. This process continues until all people have been considered for rescue.\n\n**Returning Result**: Finally, the code returns the count of boats required to rescue all people, which represents the minimum number of boats needed to accommodate everyone while adhering to the weight limit.\n\nThis approach efficiently utilizes the available boat capacity and minimizes the number of boats required to rescue all people, ensuring an optimal rescue operation.\n\n\n\n\n\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# Code\n```\nclass Solution {\npublic:\n    int numRescueBoats(vector<int>& people, int limit) {\n        int i=0,j=people.size()-1,cnt=0;\n        sort(people.begin(),people.end());\n        while(i<=j){\n            if(people[i]+people[j]<=limit){\n                i++;\n                j--;\n            }\n            else j--;\n            cnt++;\n        }\n        return cnt;\n    }\n};\n```
['C++']