|
| 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