3476. Maximize Profit from Task Assignment π
Description
You are given an integer array workers
, where workers[i]
represents the skill level of the ith
worker. You are also given a 2D integer array tasks
, where:
tasks[i][0]
represents the skill requirement needed to complete the task.tasks[i][1]
represents the profit earned from completing the task.
Each worker can complete at most one task, and they can only take a task if their skill level is equal to the task's skill requirement. An additional worker joins today who can take up any task, regardless of the skill requirement.
Return the maximum total profit that can be earned by optimally assigning the tasks to the workers.
Example 1:
Input: workers = [1,2,3,4,5], tasks = [[1,100],[2,400],[3,100],[3,400]]
Output: 1000
Explanation:
- Worker 0 completes task 0.
- Worker 1 completes task 1.
- Worker 2 completes task 3.
- The additional worker completes task 2.
Example 2:
Input: workers = [10,10000,100000000], tasks = [[1,100]]
Output: 100
Explanation:
Since no worker matches the skill requirement, only the additional worker can complete task 0.
Example 3:
Input: workers = [7], tasks = [[3,3],[3,3]]
Output: 3
Explanation:
The additional worker completes task 1. Worker 0 cannot work since no task has a skill requirement of 7.
Constraints:
1 <= workers.length <= 105
1 <= workers[i] <= 109
1 <= tasks.length <= 105
tasks[i].length == 2
1 <= tasks[i][0], tasks[i][1] <= 109
Solutions
Solution 1: Hash Table + Priority Queue
Since each task can only be completed by a worker with a specific skill, we can group the tasks by skill requirements and store them in a hash table \(\textit{d}\), where the key is the skill requirement and the value is a priority queue sorted by profit in descending order.
Then, we iterate through the workers. For each worker, we find the corresponding priority queue in the hash table \(\textit{d}\) based on their skill requirement, take the front element (i.e., the maximum profit the worker can earn), and remove it from the priority queue. If the priority queue is empty, we remove it from the hash table.
Finally, we add the maximum profit from the remaining tasks to the result.
The time complexity is \(O((n + m) \times \log m)\), and the space complexity is \(O(m)\). Where \(n\) and \(m\) are the number of workers and tasks, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|