3408. Design Task Manager
Description
There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks.
Implement the TaskManager class:
TaskManager(vector<vector<int>>& tasks)initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form[userId, taskId, priority], which adds a task to the specified user with the given priority.void add(int userId, int taskId, int priority)adds a task with the specifiedtaskIdandpriorityto the user withuserId. It is guaranteed thattaskIddoes not exist in the system.void edit(int taskId, int newPriority)updates the priority of the existingtaskIdtonewPriority. It is guaranteed thattaskIdexists in the system.void rmv(int taskId)removes the task identified bytaskIdfrom the system. It is guaranteed thattaskIdexists in the system.int execTop()executes the task with the highest priority across all users. If there are multiple tasks with the same highest priority, execute the one with the highesttaskId. After executing, thetaskIdis removed from the system. Return theuserIdassociated with the executed task. If no tasks are available, return -1.
Note that a user may be assigned multiple tasks.
Example 1:
Input:
["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"]
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]
Output:
[null, null, null, 3, null, null, 5]
Explanation
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.
taskManager.edit(102, 8); // Updates priority of task 102 to 8.
taskManager.execTop(); // return 3. Executes task 103 for User 3.
taskManager.rmv(101); // Removes task 101 from the system.
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.
taskManager.execTop(); // return 5. Executes task 105 for User 5.
Constraints:
1 <= tasks.length <= 1050 <= userId <= 1050 <= taskId <= 1050 <= priority <= 1090 <= newPriority <= 109- At most
2 * 105calls will be made in total toadd,edit,rmv, andexecTopmethods. - The input is generated such that
taskIdwill be valid.
Solutions
Solution: Priority Queue
- Time complexity: O(nlogn)
- Space complexity: O(n)
JavaScript
/**
* @param {number[][]} tasks
*/
const TaskManager = function (tasks) {
this.taskMap = new Map();
this.taskHeap = new PriorityQueue((a, b) => {
return b.priority - a.priority || b.taskId - a.taskId;
});
for (const [userId, taskId, priority] of tasks) {
this.taskMap.set(taskId, { userId, priority });
this.taskHeap.enqueue({ userId, taskId, priority });
}
};
/**
* @param {number} userId
* @param {number} taskId
* @param {number} priority
* @return {void}
*/
TaskManager.prototype.add = function (userId, taskId, priority) {
this.taskMap.set(taskId, { userId, priority });
this.taskHeap.enqueue({ userId, taskId, priority });
};
/**
* @param {number} taskId
* @param {number} newPriority
* @return {void}
*/
TaskManager.prototype.edit = function (taskId, newPriority) {
const task = this.taskMap.get(taskId);
task.priority = newPriority;
this.taskHeap.enqueue({ userId: task.userId, taskId, priority: newPriority });
};
/**
* @param {number} taskId
* @return {void}
*/
TaskManager.prototype.rmv = function (taskId) {
this.taskMap.delete(taskId);
};
/**
* @return {number}
*/
TaskManager.prototype.execTop = function () {
while (this.taskHeap.size()) {
const { userId, taskId, priority } = this.taskHeap.dequeue();
const task = this.taskMap.get(taskId);
if (task && userId === task.userId && priority === task.priority) {
this.taskMap.delete(taskId);
return userId;
}
}
return -1;
};
/**
* Your TaskManager object will be instantiated and called as such:
* var obj = new TaskManager(tasks)
* obj.add(userId,taskId,priority)
* obj.edit(taskId,newPriority)
* obj.rmv(taskId)
* var param_4 = obj.execTop()
*/