[LeetCode] Maximize Profit from Task Assignment

3476. Maximize Profit from Task Assignment

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
long long maxProfit(vector<int>& workers, vector<vector<int>>& tasks) {
unordered_map<long long, long long> w;
for(auto& worker : workers) w[worker]++;
unordered_map<long long, vector<long long>> t;
for(auto& task : tasks) t[task[0]].push_back(task[1]);
long long res = 0, additional = 0;
for(auto& [k,v] : t) {
sort(begin(v), end(v));
int cnt = w.count(k) ? w[k] : 0;
while(v.size() and cnt) {
cnt--;
res += v.back(); v.pop_back();
}
if(v.size()) additional = max(additional, v.back());
}
return res + additional;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/27/PS/LeetCode/maximize-profit-from-task-assignment/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.