-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdesign-task-manager.py
59 lines (51 loc) · 1.38 KB
/
design-task-manager.py
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
# Time: ctor: O(tlogt)
# add: O(logt)
# edit: O(logt)
# rmv: O(logt)
# execTop: O(logt)
# Space: O(t)
from sortedcontainers import SortedList
# sorted list
class TaskManager(object):
def __init__(self, tasks):
"""
:type tasks: List[List[int]]
"""
self.__lookup = {}
self.__sl = SortedList()
for userId, taskId, priority in tasks:
self.add(userId, taskId, priority)
def add(self, userId, taskId, priority):
"""
:type userId: int
:type taskId: int
:type priority: int
:rtype: None
"""
self.__sl.add((priority, taskId, userId))
self.__lookup[taskId] = (userId, priority)
def edit(self, taskId, newPriority):
"""
:type taskId: int
:type newPriority: int
:rtype: None
"""
userId, _ = self.__lookup[taskId]
self.rmv(taskId)
self.add(userId, taskId, newPriority)
def rmv(self, taskId):
"""
:type taskId: int
:rtype: None
"""
userId, priority = self.__lookup.pop(taskId)
self.__sl.remove((priority, taskId, userId))
def execTop(self):
"""
:rtype: int
"""
if not self.__sl:
return -1
_, taskId, userId = self.__sl[-1]
self.rmv(taskId)
return userId