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 1: Hash Map + Ordered Set
Thinking
Up to \(2\times 10^5\) operations require fetching the highest-priority (then highest-id) task and editing or deleting by \(\textit{taskId}\). Scanning every task on \(\textit{execTop}\) is too slow.
A hash map finds a task's user and priority in \(O(1)\) but not the global maximum. A heap or ordered set keeps the maximum, but edits must locate the old tuple.
We store \(\textit{taskId}\mapsto(\textit{userId},\textit{priority})\) in a hash map \(\textit{d}\), and \((-\textit{priority},-\textit{taskId})\) in an ordered set so the best task sits at the front. Each update touches both structures; \(\textit{execTop}\) pops the first set element.
We use a hash map \(\text{d}\) to store task information, where the key is the task ID and the value is a tuple \((\text{userId}, \text{priority})\) representing the user ID and the priority of the task.
We use an ordered set \(\text{st}\) to store all tasks currently in the system, where each element is a tuple \((-\text{priority}, -\text{taskId})\) representing the negative priority and negative task ID. We use negative values so that the task with the highest priority and largest task ID appears first in the ordered set.
For each operation, we can process as follows:
- Initialization: For each task \((\text{userId}, \text{taskId}, \text{priority})\), add it to the hash map \(\text{d}\) and the ordered set \(\text{st}\).
- Add Task: Add the task \((\text{userId}, \text{taskId}, \text{priority})\) to the hash map \(\text{d}\) and the ordered set \(\text{st}\).
- Edit Task: Retrieve the user ID and old priority for the given task ID from the hash map \(\text{d}\), remove the old task information from the ordered set \(\text{st}\), then add the new task information to both the hash map and the ordered set.
- Remove Task: Retrieve the priority for the given task ID from the hash map \(\text{d}\), remove the task information from the ordered set \(\text{st}\), and delete the task from the hash map.
- Execute Top Priority Task: If the ordered set \(\text{st}\) is empty, return -1. Otherwise, take the first element from the ordered set, get the task ID, retrieve the corresponding user ID from the hash map, and remove the task from both the hash map and the ordered set. Finally, return the user ID.
For time complexity, initialization requires \(O(n \log n)\) time, where \(n\) is the number of initial tasks. Each add, edit, remove, and execute operation requires \(O(\log m)\) time, where \(m\) is the current number of tasks in the system. Since the total number of operations does not exceed \(2 \times 10^5\), the overall time complexity is acceptable. The space complexity is \(O(n + m)\) for storing the hash map and ordered set.
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 | |
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 | |
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 | |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
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 | |