- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 47 lines of Python from the credited upstream file 3408.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from dataclasses import dataclass2from sortedcontainers import SortedDict, SortedSet3 4 5@dataclass(frozen=True)6class Task:7 userId: int8 taskId: int9 priority: int10 11 def __lt__(self, other):12 if self.priority == other.priority:13 return self.taskId > other.taskId14 return self.priority > other.priority15 16 17class TaskManager:18 def __init__(self, tasks: list[list[int]]):19 self.taskMap = SortedDict() 20 self.taskSet = SortedSet() 21 for task in tasks:22 self.add(task[0], task[1], task[2])23 24 def add(self, userId: int, taskId: int, priority: int) -> None:25 task = Task(userId, taskId, priority)26 self.taskMap[taskId] = task27 self.taskSet.add(task)28 29 def edit(self, taskId: int, newPriority: int) -> None:30 task = self.taskMap[taskId]31 self.taskSet.remove(task)32 editedTask = Task(task.userId, taskId, newPriority)33 self.taskSet.add(editedTask)34 self.taskMap[taskId] = editedTask35 36 def rmv(self, taskId: int) -> None:37 task = self.taskMap[taskId]38 self.taskSet.remove(task)39 del self.taskMap[taskId]40 41 def execTop(self):42 if not self.taskSet:43 return -144 task = self.taskSet.pop(0)45 del self.taskMap[task.taskId]46 return task.userId47