-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path3476-maximize-profit-from-task-assignment.js
More file actions
57 lines (50 loc) · 1.63 KB
/
3476-maximize-profit-from-task-assignment.js
File metadata and controls
57 lines (50 loc) · 1.63 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 3476. Maximize Profit from Task Assignment
* https://leetcode.com/problems/maximize-profit-from-task-assignment/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} workers
* @param {number[][]} tasks
* @return {number}
*/
var maxProfit = function(workers, tasks) {
const map = new Map();
for (const [skill, profit] of tasks) {
if (!map.has(skill)) {
map.set(skill, []);
}
map.get(skill).push(profit);
}
for (const [skill, profits] of map) {
profits.sort((a, b) => b - a);
}
let totalProfit = 0;
for (const worker of workers) {
if (map.has(worker) && map.get(worker).length > 0) {
totalProfit += map.get(worker).shift();
if (map.get(worker).length === 0) {
map.delete(worker);
}
}
}
let maxRemainingProfit = 0;
for (const profits of map.values()) {
if (profits.length > 0) {
maxRemainingProfit = Math.max(maxRemainingProfit, profits[0]);
}
}
return totalProfit + maxRemainingProfit;
};