You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
我们用一个哈希表 $\text{d}$ 来存储任务信息,键为任务 ID,值为一个二元组 $(\text{userId}, \text{priority})$,表示该任务所属的用户 ID 以及任务的优先级。
92
+
93
+
我们用一个有序集合 $\text{st}$ 来存储当前系统中的所有任务,元素为一个二元组 $(-\text{priority}, -\text{taskId})$,表示任务的优先级和任务 ID 的相反数。我们将优先级和任务 ID 取相反数是为了让优先级最高且任务 ID 最大的任务在有序集合中排在最前面。
Copy file name to clipboardExpand all lines: solution/3400-3499/3408.Design Task Manager/README_EN.md
+148-1Lines changed: 148 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,7 +84,21 @@ taskManager.execTop(); // return 5. Executes task 105 for User 5.</div>
84
84
85
85
<!-- solution:start -->
86
86
87
-
### Solution 1
87
+
### Solution 1: Hash Map + Ordered Set
88
+
89
+
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.
90
+
91
+
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.
92
+
93
+
For each operation, we can process as follows:
94
+
95
+
-**Initialization**: For each task $(\text{userId}, \text{taskId}, \text{priority})$, add it to the hash map $\text{d}$ and the ordered set $\text{st}$.
96
+
-**Add Task**: Add the task $(\text{userId}, \text{taskId}, \text{priority})$ to the hash map $\text{d}$ and the ordered set $\text{st}$.
97
+
-**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.
98
+
-**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.
99
+
-**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.
100
+
101
+
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.
0 commit comments