aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-09-17 20:30:06 -0600
committerJack Sangdahl <[email protected]>2025-09-17 20:30:06 -0600
commitb07d9a7387ba1b74698af076782b27fd773a696e (patch)
tree19f91ad1e270be6fb687f20f9f1c68a8e3a1dd6c
parentff1952c4a397b91945686c74e8e86e4dd4c4e7d2 (diff)
downloadleetcode-b07d9a7387ba1b74698af076782b27fd773a696e.tar.gz
leetcode-b07d9a7387ba1b74698af076782b27fd773a696e.zip
3408. design task manager
-rw-r--r--design_task_manager.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/design_task_manager.py b/design_task_manager.py
new file mode 100644
index 0000000..11e4c3a
--- /dev/null
+++ b/design_task_manager.py
@@ -0,0 +1,64 @@
+# 3408. Design Task Manager
+import heapq
+
+
+class TaskManager(object):
+ def __init__(self, tasks):
+ """
+ :type tasks: List[List[int]]
+ """
+ self.heap = []
+ self.task_priority = {}
+ self.task_owner = {}
+ for i in tasks:
+ self.add(i[0], i[1], i[2])
+
+ def add(self, userId, taskId, priority):
+ """
+ :type userId: int
+ :type taskId: int
+ :type priority: int
+ :rtype: None
+ """
+ heapq.heappush(self.heap, (-priority, -taskId))
+ self.task_priority[taskId] = priority
+ self.task_owner[taskId] = userId
+
+ def edit(self, taskId, newPriority):
+ """
+ :type taskId: int
+ :type newPriority: int
+ :rtype: None
+ """
+ heapq.heappush(self.heap, (-newPriority, -taskId))
+ self.task_priority[taskId] = newPriority
+
+ def rmv(self, taskId):
+ """
+ :type taskId: int
+ :rtype: None
+ """
+ self.task_priority[taskId] = -1
+
+ def execTop(self):
+ """
+ :rtype: int
+ """
+ while self.heap:
+ neg_priority, neg_task_id = heapq.heappop(self.heap)
+ priority = -neg_priority
+ task_id = -neg_task_id
+ if self.task_priority.get(task_id, -2) == priority:
+ self.task_priority[task_id] = -1
+ return self.task_owner.get(task_id, -1)
+ return -1
+
+
+if __name__ == "__main__":
+ obj = TaskManager(tasks=[[1, 101, 10], [2, 102, 20], [3, 103, 15]])
+ obj.add(4, 104, 5)
+ obj.edit(102, 8)
+ print(obj.execTop())
+ obj.rmv(101)
+ obj.add(5, 105, 15)
+ print(obj.execTop())