|
| 1 | +import collections |
| 2 | +import heapq |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import List |
| 5 | + |
| 6 | + |
| 7 | +@dataclass(order=True, frozen=True) |
| 8 | +class Task: |
| 9 | + priority: int |
| 10 | + taskId: int |
| 11 | + userId: int |
| 12 | + |
| 13 | + |
| 14 | +class TaskManager: |
| 15 | + """ |
| 16 | + There is a task management system that allows users to manage their tasks, each |
| 17 | + associated with a priority. The system should efficiently handle adding, modifying, |
| 18 | + executing, and removing tasks. |
| 19 | +
|
| 20 | + Implement the TaskManager class: |
| 21 | + - TaskManager(vector<vector<int>>& tasks) initializes the task manager with a list |
| 22 | + of user-task-priority triples. Each element in the input list is of the form |
| 23 | + [userId, taskId, priority], which adds a task to the specified user with the given |
| 24 | + priority. |
| 25 | + - void add(int userId, int taskId, int priority) adds a task with the specified |
| 26 | + taskId and priority to the user with userId. It is guaranteed that taskId does not |
| 27 | + exist in the system. |
| 28 | + - void edit(int taskId, int newPriority) updates the priority of the existing |
| 29 | + taskId to newPriority. It is guaranteed that taskId exists in the system. |
| 30 | + - void rmv(int taskId) removes the task identified by taskId from the system. It is |
| 31 | + guaranteed that taskId exists in the system. |
| 32 | + - int execTop() executes the task with the highest priority across all users. If |
| 33 | + there are multiple tasks with the same highest priority, execute the one with the |
| 34 | + highest taskId. After executing, the taskId is removed from the system. Return the |
| 35 | + userId associated with the executed task. If no tasks are available, return -1. |
| 36 | +
|
| 37 | + Note that a user may be assigned multiple tasks. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self, tasks: List[List[int]]): |
| 41 | + # (taskId -> userId) |
| 42 | + self.task_id_to_user_id = collections.defaultdict(int) |
| 43 | + self.task_id_to_priority = collections.defaultdict(int) |
| 44 | + self.heap = [] |
| 45 | + for task in tasks: |
| 46 | + userId, taskId, priority = task |
| 47 | + task = Task(-priority, -taskId, -userId) |
| 48 | + self.task_id_to_user_id[taskId] = userId |
| 49 | + self.task_id_to_priority[taskId] = priority |
| 50 | + heapq.heappush(self.heap, task) |
| 51 | + |
| 52 | + def add(self, userId: int, taskId: int, priority: int) -> None: |
| 53 | + # O(1) |
| 54 | + task = Task(-priority, -taskId, -userId) |
| 55 | + self.task_id_to_user_id[taskId] = userId |
| 56 | + self.task_id_to_priority[taskId] = priority |
| 57 | + heapq.heappush(self.heap, task) |
| 58 | + |
| 59 | + def edit(self, taskId: int, newPriority: int) -> None: |
| 60 | + # O(1) |
| 61 | + userId = self.task_id_to_user_id[taskId] |
| 62 | + task = Task(-newPriority, -taskId, -userId) |
| 63 | + self.task_id_to_priority[taskId] = newPriority |
| 64 | + heapq.heappush(self.heap, task) |
| 65 | + |
| 66 | + def rmv(self, taskId: int) -> None: |
| 67 | + # O(1) |
| 68 | + del self.task_id_to_user_id[taskId] |
| 69 | + del self.task_id_to_priority[taskId] |
| 70 | + |
| 71 | + def execTop(self) -> int: |
| 72 | + # O(log(n)) |
| 73 | + # O(1) |
| 74 | + while self.heap: |
| 75 | + task = self.heap[0] |
| 76 | + if -task.taskId not in self.task_id_to_priority: |
| 77 | + heapq.heappop(self.heap) |
| 78 | + continue |
| 79 | + if -task.priority != self.task_id_to_priority[-task.taskId]: |
| 80 | + heapq.heappop(self.heap) |
| 81 | + continue |
| 82 | + break |
| 83 | + if self.heap: |
| 84 | + task = heapq.heappop(self.heap) |
| 85 | + self.rmv(-task.taskId) |
| 86 | + return -task.userId |
| 87 | + return -1 |
| 88 | + |
| 89 | + |
| 90 | +# Your TaskManager object will be instantiated and called as such: |
| 91 | +# obj = TaskManager(tasks) |
| 92 | +# obj.add(userId,taskId,priority) |
| 93 | +# obj.edit(taskId,newPriority) |
| 94 | +# obj.rmv(taskId) |
| 95 | +# param_4 = obj.execTop() |
0 commit comments