Simple Straightforward Binary Search Solution | C++
Approach\nSorting and Maximizing Profit:\n\nFirst, the difficulty and profit vectors are combined into a vector of pairs (vtr). Each pair consists of a difficul
# Approach\nSorting and Maximizing Profit:\n\nFirst, the difficulty and profit vectors are combined into a vector of pairs (vtr). Each pair consists of a difficulty level and the corresponding profit.\nThis vector of pairs (vtr) is then sorted based on the difficulty levels.\n\nMaximizing Profit Calculation:\n\nAs the vector vtr is sorted, a variable maxi is used to keep track of the maximum profit encountered so far.\nFor each pair in vtr, update the profit to be the maximum profit encountered up to that point (maxi). This ensures that for any given difficulty level, vtr[i].second holds the maximum profit that can be achieved up to that difficulty level.\n\nAssigning Profits to Workers:\n\nFor each worker\'s capability (stored in worker vector), use binary search (lower_bound) to find the highest difficulty level that the worker can handle efficiently.\nAccumulate the maximum profit corresponding to the highest difficulty level that the worker can handle into sum.\n\n# Complexity\n- Time complexity:\nO(NlogN)\n\n- Space complexity:\nO(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();\n       vector<pair<int,int>> vtr;\n       for(int i=0;i<n;i++){\n        vtr.push_back({difficulty[i],profit[i]});\n       }\n       sort(vtr.begin(),vtr.end());\n       int maxi=vtr[0].second;\n       for(auto &it:vtr){\n        maxi=max(maxi,it.second);\n        it.second=maxi;\n       }       \n       int sum=0;\n       for(auto &it:worker){\n         int val=it,curr=0;\n         int l=0,h=n-1;\n         while(h>=l){\n            int mid=(h+l)/2;\n            if(vtr[mid].first<=val){\n               curr=vtr[mid].second;\n               l=mid+1;\n            }\n            else h=mid-1;\n         }\n         sum+=curr;\n       }\n\n       return sum;\n    }\n};\n```
['C++']