LeetCode 题解工作台
设计任务管理器
一个任务管理器系统可以让用户管理他们的任务,每个任务有一个优先级。这个系统需要高效地处理添加、修改、执行和删除任务的操作。 请你设计一个 TaskManager 类: TaskManager(vector >& tasks) 初始化任务管理器,初始化的数组格式为 [userId, taskId, p…
4
题型
5
代码语言
3
相关题
当前训练重点
中等 · 堆
答案摘要
我们用一个哈希表 来存储任务信息,键为任务 ID,值为一个二元组 $(\text{userId}, \text{priority})$,表示该任务所属的用户 ID 以及任务的优先级。 我们用一个有序集合 来存储当前系统中的所有任务,元素为一个二元组 $(-\text{priority}, -\text{taskId})$,表示任务的优先级和任务 ID 的相反数。我们将优先级和任务 ID 取相反…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 堆 题型思路
题目描述
一个任务管理器系统可以让用户管理他们的任务,每个任务有一个优先级。这个系统需要高效地处理添加、修改、执行和删除任务的操作。
请你设计一个 TaskManager 类:
-
TaskManager(vector<vector<int>>& tasks)初始化任务管理器,初始化的数组格式为[userId, taskId, priority],表示给userId添加一个优先级为priority的任务taskId。 -
void add(int userId, int taskId, int priority)表示给用户userId添加一个优先级为priority的任务taskId,输入 保证taskId不在系统中。 -
void edit(int taskId, int newPriority)更新已经存在的任务taskId的优先级为newPriority。输入 保证taskId存在于系统中。 -
void rmv(int taskId)从系统中删除任务taskId。输入 保证taskId存在于系统中。 -
int execTop()执行所有用户的任务中优先级 最高 的任务,如果有多个任务优先级相同且都为 最高 ,执行taskId最大的一个任务。执行完任务后,taskId从系统中 删除 。同时请你返回这个任务所属的用户userId。如果不存在任何任务,返回 -1 。
注意 ,一个用户可能被安排多个任务。
示例 1:
输入:
["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], []]
输出:
[null, null, null, 3, null, null, 5]
解释:
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // 分别给用户 1 ,2 和 3 初始化一个任务。taskManager.add(4, 104, 5); // 给用户 4 添加优先级为 5 的任务 104 。
taskManager.edit(102, 8); // 更新任务 102 的优先级为 8 。
taskManager.execTop(); // 返回 3 。执行用户 3 的任务 103 。
taskManager.rmv(101); // 将系统中的任务 101 删除。
taskManager.add(5, 105, 15); // 给用户 5 添加优先级为 15 的任务 105 。
taskManager.execTop(); // 返回 5 。执行用户 5 的任务 105 。
提示:
1 <= tasks.length <= 1050 <= userId <= 1050 <= taskId <= 1050 <= priority <= 1090 <= newPriority <= 109add,edit,rmv和execTop的总操作次数 加起来 不超过2 * 105次。- 输入保证
taskId是合法的。
解题思路
方法一:哈希表 + 有序集合
我们用一个哈希表 来存储任务信息,键为任务 ID,值为一个二元组 ,表示该任务所属的用户 ID 以及任务的优先级。
我们用一个有序集合 来存储当前系统中的所有任务,元素为一个二元组 ,表示任务的优先级和任务 ID 的相反数。我们将优先级和任务 ID 取相反数是为了让优先级最高且任务 ID 最大的任务在有序集合中排在最前面。
对于每个操作,我们可以按如下方式进行处理:
- 初始化:对于每个任务 ,我们将其添加到哈希表 和有序集合 中。
- 添加任务:将任务 添加到哈希表 和有序集合 中。
- 编辑任务:从哈希表 中获取任务 ID 对应的用户 ID 和旧优先级,然后从有序集合 中删除旧的任务信息,再将新的任务信息添加到哈希表 和有序集合 中。
- 删除任务:从哈希表 中获取任务 ID 对应的优先级,然后从有序集合 中删除任务信息,并从哈希表 中删除任务。
- 执行最高优先级任务:如果有序集合 为空,返回 -1。否则,从有序集合 中取出第一个元素,获取任务 ID,然后从哈希表 中获取对应的用户 ID,并将任务从哈希表 和有序集合 中删除,最后返回用户 ID。
时间复杂度方面,初始化操作需要 的时间,其中 是初始任务的数量。每个添加、编辑、删除和执行操作都需要 的时间,其中 是当前系统中的任务数量。由于总操作次数不超过 ,因此整体时间复杂度是可接受的。空间复杂度 ,用于存储哈希表和有序集合。
class TaskManager:
def __init__(self, tasks: List[List[int]]):
self.d = {}
self.st = SortedList()
for task in tasks:
self.add(*task)
def add(self, userId: int, taskId: int, priority: int) -> None:
self.d[taskId] = (userId, priority)
self.st.add((-priority, -taskId))
def edit(self, taskId: int, newPriority: int) -> None:
userId, priority = self.d[taskId]
self.st.discard((-priority, -taskId))
self.d[taskId] = (userId, newPriority)
self.st.add((-newPriority, -taskId))
def rmv(self, taskId: int) -> None:
_, priority = self.d[taskId]
self.d.pop(taskId)
self.st.remove((-priority, -taskId))
def execTop(self) -> int:
if not self.st:
return -1
taskId = -self.st.pop(0)[1]
userId, _ = self.d[taskId]
self.d.pop(taskId)
return userId
# 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()
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Understanding how to combine a hash table for storage and a heap for priority management will be crucial.
- question_mark
The candidate should be able to explain how the operations affect the time complexity.
- question_mark
Efficient memory management while performing these operations should be discussed, especially with large inputs.
常见陷阱
外企场景- error
Failing to update the heap properly when task priorities change can lead to incorrect execution order.
- error
Not considering edge cases such as executing or removing tasks from an empty system.
- error
Overcomplicating the data structures or operations instead of using a simple Hash Table and Heap combination.
进阶变体
外企场景- arrow_right_alt
Implementing a Task Manager that handles multiple users with different priorities for tasks.
- arrow_right_alt
Expanding the system to support additional operations such as querying the number of tasks left with certain priorities.
- arrow_right_alt
Optimizing the Task Manager to handle larger datasets with reduced time complexity for each operation.