Design Task Manager Problem
#3408
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3408.
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 <= 10^50 <= userId <= 10^50 <= taskId <= 10^50 <= priority <= 10^90 <= newPriority <= 10^9- At most
2 * 10^5calls will be made in total toadd,edit,rmv, andexecTopmethods. - The input is generated such that
taskIdwill be valid.
Solution
Approach: Hash Map + Max-Heap with Lazy Deletion
Keep a hash map d from task id to (userId, priority) for the tasks that currently exist, and a max-heap of (priority, taskId) pairs, ordered by priority and then by task id (both larger first).
- Initialize / add: store the task in
dand push(priority, taskId)onto the heap. - edit: update the priority in
dand push the new pair. The old pair stays in the heap but is now out of date. - rmv: delete the task from
d. Its heap entries become out of date. - execTop: pop pairs from the heap until one is up to date, meaning the task is still in
dwith the same priority. Delete it fromdand return its user. If the heap runs empty, return-1.
Every pair is pushed once and popped at most once, so out-of-date entries cost nothing extra.
Complexity: O((n + q) log (n + q)) total time for n initial tasks and q operations, O(n + q) space.
import heapq
class TaskManager:
def __init__(self, tasks: List[List[int]]):
self.d = {}
self.h = []
for userId, taskId, priority in tasks:
self.add(userId, taskId, priority)
def add(self, userId: int, taskId: int, priority: int) -> None:
self.d[taskId] = (userId, priority)
heapq.heappush(self.h, (-priority, -taskId))
def edit(self, taskId: int, newPriority: int) -> None:
userId, _ = self.d[taskId]
self.d[taskId] = (userId, newPriority)
heapq.heappush(self.h, (-newPriority, -taskId))
def rmv(self, taskId: int) -> None:
del self.d[taskId]
def execTop(self) -> int:
while self.h:
p, t = heapq.heappop(self.h)
p, t = -p, -t
if t in self.d and self.d[t][1] == p:
userId = self.d[t][0]
del self.d[t]
return userId
return -1
# Your TaskManager object will be instantiated and called as such:
# obj = TaskManager(tasks)
# obj.add(userId,taskId,priority)
# obj.edit(taskId,newPriority)
# obj.rmv(taskId)
# param_4 = obj.execTop()