💯✅🔥Easy Java ,Python3 ,C++ Solution|| 30 ms ||≧◠‿◠≦✌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is about assigning tasks to workers based on their skills. Each task has a 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is about assigning tasks to workers based on their skills. Each task has a difficulty level and a corresponding profit. The goal is to maximize the total profit by assigning tasks to workers based on their skills.\nThe approach is to first sort the tasks by difficulty and then iterate over the workers. For each worker, find the maximum profit that can be earned by assigning tasks up to the worker\'s skill level. This is done by performing a binary search on the sorted list of tasks to find the maximum profit that can be earned.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- **Sorting**: Sort the tasks by difficulty level.\n- **Binary Search**: For each worker, perform a binary search on the sorted list of tasks to find the maximum profit that can be earned by assigning tasks up to the worker\'s skill level.\n- **Assignment**: Assign tasks to workers based on their skills and the maximum profit that can be earned.\n# Complexity\n- Time complexity:O(n log n)\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```java []\nclass Solution {\n    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {\n        List<int[]> infos = new ArrayList<>();\n        for (int i = 0; i < difficulty.length; i++) {\n            infos.add(new int[]{difficulty[i], profit[i]});\n        }\n        infos.sort((a, b) -> a[0] - b[0]);\n        \n        List<int[]> maxProfitForDifficulty = new ArrayList<>();\n        int maxp = 0;\n        for (int[] info : infos) {\n            int d = info[0], p = info[1];\n            maxp = Math.max(maxp, p);\n            maxProfitForDifficulty.add(new int[]{d, maxp});\n        }\n        \n        int totalProfit = 0;\n        for (int w : worker) {\n            int ind = binarySearch(maxProfitForDifficulty, w);\n            if (ind >= 0 && w >= maxProfitForDifficulty.get(ind)[0]) {\n                totalProfit += maxProfitForDifficulty.get(ind)[1];\n            }\n        }\n        return totalProfit;\n    }\n    \n    private int binarySearch(List<int[]> list, int target) {\n        int left = 0, right = list.size() - 1;\n        while (left <= right) {\n            int mid = left + (right - left) / 2;\n            if (list.get(mid)[0] <= target) {\n                left = mid + 1;\n            } else {\n                right = mid - 1;\n            }\n        }\n        return right;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:\n        infos = sorted(zip(difficulty, profit))\n        max_profit_for_difficulty = []\n        max_profit = 0\n        for d, p in infos:\n            max_profit = max(max_profit, p)\n            max_profit_for_difficulty.append((d, max_profit))\n        \n        total_profit = 0\n        for w in worker:\n            index = self.binary_search(max_profit_for_difficulty, w)\n            if index >= 0 and w >= max_profit_for_difficulty[index][0]:\n                total_profit += max_profit_for_difficulty[index][1]\n        return total_profit\n    \n    def binary_search(self, max_profit_for_difficulty: List[Tuple[int, int]], target: int) -> int:\n        left, right = 0, len(max_profit_for_difficulty) - 1\n        while left <= right:\n            mid = (left + right) // 2\n            if max_profit_for_difficulty[mid][0] <= target:\n                left = mid + 1\n            else:\n                right = mid - 1\n        return right\n```\n```CPP []\nclass Solution {\npublic:\n    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {\n        vector<pair<int, int>> infos;\n        for (int i = 0; i < difficulty.size(); i++) {\n            infos.emplace_back(difficulty[i], profit[i]);\n        }\n        sort(infos.begin(), infos.end());\n        \n        vector<pair<int, int>> maxProfitForDifficulty;\n        int maxp = 0;\n        for (auto& info : infos) {\n            int d = info.first, p = info.second;\n            maxp = max(maxp, p);\n            maxProfitForDifficulty.emplace_back(d, maxp);\n        }\n        \n        int totalProfit = 0;\n        for (int w : worker) {\n            int ind = binarySearch(maxProfitForDifficulty, w);\n            if (ind >= 0 && w >= maxProfitForDifficulty[ind].first) {\n                totalProfit += maxProfitForDifficulty[ind].second;\n            }\n        }\n        return totalProfit;\n    }\n    \nprivate:\n    int binarySearch(vector<pair<int, int>>& list, int target) {\n        int left = 0, right = list.size() - 1;\n        while (left <= right) {\n            int mid = left + (right - left) / 2;\n            if (list[mid].first <= target) {\n                left = mid + 1;\n            } else {\n                right = mid - 1;\n            }\n        }\n        return right;\n    }\n};\n```\n\n![upvote.png](https://assets.leetcode.com/users/images/341c5029-b77a-4dfc-ab72-cdc3e834dfa1_1718669627.8883066.png)\n
['Binary Search', 'C++', 'Java', 'Python3']