sort and search ✅ || beginner-friendly solution with clear intuition ✅
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can start by creating a 2D array work that pairs each job\'s difficulty with its cor
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can start by creating a 2D array `work` that pairs each job\'s `difficulty` with its corresponding `profit`. Next, we sort this array in ascending order based on `difficulty`. For each worker, we then perform a **floor binary search** to identify the most difficult task they can handle. This allows us to determine the maximum profit they can earn from any task up to that point. Since the worker can undertake any job up to the binary search position, this approach ensures they are matched with the highest profit available within their capability.\n\n\n# Example Explanation\n<!-- Describe your approach to solving the problem. -->\n![jun-18.jpg](https://assets.leetcode.com/users/images/809384aa-85bc-467c-ae37-d35f38d786e6_1718687790.3698478.jpeg)\n\n\n# Complexity\n- Time complexity:$$O(nlogn + mlogn)$$\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 \n{\n    public int floorBinSearch(int[][] work, int worker, int n)\n    {\n        int ind = -1;\n        int low = 0, high = n-1;\n        while(low <= high)\n        {\n            int mid = (low+high)/2;\n            if(work[mid][0] <= worker)\n            {\n                ind = mid;\n                low = mid + 1;\n            }\n            else if(work[mid][0] > worker)\n            {\n                high = mid - 1;\n            }\n        }\n        return ind;\n    }\n    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) \n    {\n        int totalProfit = 0;\n        int n = profit.length;\n        int m = worker.length;\n        int[][] work = new int[n][2];\n        for(int i = 0; i < n; i ++)\n        {\n            work[i][0] = difficulty[i];\n            work[i][1] = profit[i];\n        }\n        Arrays.sort(work, Comparator.comparingDouble(o -> o[0]));\n        int[] prefixProfit = new int[n];\n        for(int i = 0, mp = Integer.MIN_VALUE; i < n; i ++)\n        {\n            mp = Math.max(mp, work[i][1]);\n            work[i][1] = mp;\n        }\n        for(int i = 0; i < m; i ++)\n        {\n            int ind = floorBinSearch(work, worker[i], n);\n            if(ind != -1)\n            {\n                totalProfit = totalProfit + work[ind][1];\n            }\n        }\n        return totalProfit;\n    }\n}\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/600f9b81-e7e2-4f50-a9b6-016e42d3bbb0_1718687852.4633694.jpeg)\n
['Binary Search', 'Sorting', 'Java']