C++ Easy to UnderStand Sorting solution || ⭐⭐⭐
Intuition\n Describe your first thoughts on how to solve this problem. \nSort\n\n# Approach\n Describe your approach to solving the problem. \n1. make vector{pr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSort\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. make **vector{profit,difficulty}** and sort it \n2. sort worker array \n3. keep index at last of sorted vector{profit,difficulty}\n4. **start from last of sorted worker array** and check for worker[i]>=difficulty of sorted array{profit,difficulty} \n4,1. If not greater, do index-- till greater found and add that profit to final ans **because profits beyond that cannot be acquired by other workers futher in iteration** \n4,2. Else add vector{profit,difficulty}[index] to finalAns\n5. Return final Answer\n \n\n\n# Complexity\nlength of difficulty array=n\nlength of worker array=m\n- Time complexity:O(nlogn+mlogm+n+m) \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker){\n        int n=difficulty.size();\n        vector<pair<int,int>> nums;\n\n        for(int i=0;i<n;i++){\n            nums.push_back({profit[i],difficulty[i]});\n        }\n        sort(nums.begin(),nums.end());\n        int index=n-1;\n        int ans=0;\n        sort(worker.begin(),worker.end());\n        \n        for(int i=worker.size()-1;i>=0;i--){\n            while(index>=0 && nums[index].second>worker[i]) index--;\n            if(index==-1) continue;\n            ans+=nums[index].first;\n        }\n        return ans;        \n    }\n};\n```
['Sorting', 'C++']