Easy C++ Solution | Code Explained | Binary Search | Sorting | Beats 93.58%
Intuition and Approach\n\nWe can calculate the maximum profit that can be obtained by assigning workers to jobs based on their difficulty levels and correspondi
# Intuition and Approach\n\nWe can calculate the maximum profit that can be obtained by assigning workers to jobs based on their difficulty levels and corresponding profits. \n\n-- Sort jobs based on their difficulty and store the corresponding profits. \n\n1) Create a vector of pairs, where each pair represents the difficulty and profit of a job.\n\n2) Sort the vector based on difficulty levels.\n\n3) Calculate the maximum profit achievable at each difficulty level and store it in the maxProfit array. To find the highest difficulty level they can handle, use binary search.\n\n4) Add the maximum profit achievable up to that difficulty level to the result.\n\n---------------------------------------------------------------------\n\n# Complexity\n- Time complexity: O(N * logN + M * log N)\n-- where N is the number of jobs and M is the number of workers.\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-------------------------------------------------------------------\n\n### If you find this solution helpful, consider giving it an upvote!\n\n\n#### Do check out all other solutions posted by me here: https://github.com/adityajamwal02/Leetcode-Community-Solutions\n\n-------------------------------------------------------------------\n\n# Code\n```\nclass Solution {\npublic:\n    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {\n        int n=profit.size(), m=worker.size();\n        vector<pair<int, int>> v;\n        for(int i=0;i<n;i++){\n            v.push_back({difficulty[i], profit[i]});\n        }\n        sort(v.begin(), v.end());\n        vector<int> maxProfit(n);\n        maxProfit[0]=v[0].second;\n        for(int i=1;i<n;i++){\n            maxProfit[i]=max(maxProfit[i-1], v[i].second);\n        }\n        int result=0;\n        for(int i=0;i<m;i++){\n            int low=0, high=n;\n            while(high-low>1){\n                int mid=low+(high-low)/2;\n                if(v[mid].first<=worker[i]){\n                    low=mid;\n                }else{\n                    high=mid;\n                }\n            }\n            int idx = high-1;\n            if(v[idx].first<=worker[i])\n                result+=maxProfit[idx];\n        }\n    return result;\n    }\n};\n```
['Two Pointers', 'Binary Search', 'Sorting', 'C++']