Skip to content

Commit 4fec9f5

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 3408
1 parent ac97fdb commit 4fec9f5

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
- [3136 Valid Word](https://leetcode.com/problems/valid-word/description/)
303303
- [3202 Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/description/)
304304
- [3392 Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/description/)
305+
- [3408 Design Task Manager](https://leetcode.com/problems/design-task-manager/description/)
305306
- [3446 Sort Matrix by Diagonals](https://leetcode.com/problems/sort-matrix-by-diagonals/description/)
306307

307308
## Development 🔧
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from awesome_python_leetcode._3408_design_task_manager import TaskManager
2+
3+
4+
def test_func():
5+
"""Tests the solution of a LeetCode problem."""
6+
manager = TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]])
7+
8+
manager.add(4, 104, 5)
9+
manager.edit(102, 8)
10+
assert manager.execTop() == 3
11+
12+
manager.rmv(101)
13+
manager.add(5, 105, 15)
14+
assert manager.execTop() == 5

0 commit comments

Comments
 (0)