Skip to content

Commit 9a72139

Browse files
committed
Add solution #3408
1 parent 600214d commit 9a72139

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,7 @@
27322732
3403|[Find the Lexicographically Largest String From the Box I](./solutions/3403-find-the-lexicographically-largest-string-from-the-box-i.js)|Medium|
27332733
3405|[Count the Number of Arrays with K Matching Adjacent Elements](./solutions/3405-count-the-number-of-arrays-with-k-matching-adjacent-elements.js)|Hard|
27342734
3406|[Find the Lexicographically Largest String From the Box II](./solutions/3406-find-the-lexicographically-largest-string-from-the-box-ii.js)|Hard|
2735+
3408|[Design Task Manager](./solutions/3408-design-task-manager.js)|Medium|
27352736
3416|[Subsequences with a Unique Middle Mode II](./solutions/3416-subsequences-with-a-unique-middle-mode-ii.js)|Hard|
27362737
3422|[Minimum Operations to Make Subarray Elements Equal](./solutions/3422-minimum-operations-to-make-subarray-elements-equal.js)|Medium|
27372738
3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy|
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* 3408. Design Task Manager
3+
* https://leetcode.com/problems/design-task-manager/
4+
* Difficulty: Medium
5+
*
6+
* There is a task management system that allows users to manage their tasks, each associated
7+
* with a priority. The system should efficiently handle adding, modifying, executing, and
8+
* removing tasks.
9+
*
10+
* Implement the TaskManager class:
11+
* - TaskManager(vector<vector<int>>& tasks) initializes the task manager with a list of
12+
* user-task-priority triples. Each element in the input list is of the form [userId,
13+
* taskId, priority], which adds a task to the specified user with the given priority.
14+
* - void add(int userId, int taskId, int priority) adds a task with the specified taskId
15+
* and priority to the user with userId. It is guaranteed that taskId does not exist in
16+
* the system.
17+
* - void edit(int taskId, int newPriority) updates the priority of the existing taskId to
18+
* newPriority. It is guaranteed that taskId exists in the system.
19+
* - void rmv(int taskId) removes the task identified by taskId from the system. It is
20+
* guaranteed that taskId exists in the system.
21+
* - int execTop() executes the task with the highest priority across all users. If there
22+
* are multiple tasks with the same highest priority, execute the one with the highest
23+
* taskId. After executing, the taskId is removed from the system. Return the userId
24+
* associated with the executed task. If no tasks are available, return -1.
25+
*
26+
* Note that a user may be assigned multiple tasks.
27+
*/
28+
29+
/**
30+
* @param {number[][]} tasks
31+
*/
32+
var TaskManager = function(tasks) {
33+
this.taskRegistry = {};
34+
this.priorityHeap = new PriorityQueue((a, b) => {
35+
return a.priority !== b.priority ? b.priority - a.priority : b.taskId - a.taskId;
36+
});
37+
38+
for (const [userId, taskId, priority] of tasks) {
39+
this.taskRegistry[taskId] = { userId, priority, version: 1 };
40+
this.priorityHeap.enqueue({ taskId, priority, version: 1 });
41+
}
42+
};
43+
44+
/**
45+
* @param {number} userId
46+
* @param {number} taskId
47+
* @param {number} priority
48+
* @return {void}
49+
*/
50+
TaskManager.prototype.add = function(userId, taskId, priority) {
51+
this.taskRegistry[taskId] = { userId, priority, version: 1 };
52+
this.priorityHeap.enqueue({ taskId, priority, version: 1 });
53+
};
54+
55+
/**
56+
* @param {number} taskId
57+
* @param {number} newPriority
58+
* @return {void}
59+
*/
60+
TaskManager.prototype.edit = function(taskId, newPriority) {
61+
const currentTask = this.taskRegistry[taskId];
62+
currentTask.priority = newPriority;
63+
currentTask.version++;
64+
this.priorityHeap.enqueue({ taskId, priority: newPriority, version: currentTask.version });
65+
};
66+
67+
/**
68+
* @param {number} taskId
69+
* @return {void}
70+
*/
71+
TaskManager.prototype.rmv = function(taskId) {
72+
delete this.taskRegistry[taskId];
73+
};
74+
75+
76+
/**
77+
* @return {number}
78+
*/
79+
TaskManager.prototype.execTop = function() {
80+
while (!this.priorityHeap.isEmpty()) {
81+
const { taskId, priority, version } = this.priorityHeap.dequeue();
82+
const activeTask = this.taskRegistry[taskId];
83+
84+
if (activeTask && activeTask.priority === priority && activeTask.version === version) {
85+
const executedUserId = activeTask.userId;
86+
delete this.taskRegistry[taskId];
87+
return executedUserId;
88+
}
89+
}
90+
return -1;
91+
};

0 commit comments

Comments
 (0)