Skip to content

Commit e19bb83

Browse files
committed
[LeetCode Sync] Runtime - 443 ms (90.57%), Memory - 105.8 MB (32.83%)
1 parent 6b99200 commit e19bb83

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

3678-design-task-manager/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<p>There is a task management system that allows users to manage their tasks, each associated with a priority. The system should efficiently handle adding, modifying, executing, and removing tasks.</p>
2+
3+
<p>Implement the <code>TaskManager</code> class:</p>
4+
5+
<ul>
6+
<li>
7+
<p><code>TaskManager(vector&lt;vector&lt;int&gt;&gt;&amp; tasks)</code> initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form <code>[userId, taskId, priority]</code>, which adds a task to the specified user with the given priority.</p>
8+
</li>
9+
<li>
10+
<p><code>void add(int userId, int taskId, int priority)</code> adds a task with the specified <code>taskId</code> and <code>priority</code> to the user with <code>userId</code>. It is <strong>guaranteed</strong> that <code>taskId</code> does not <em>exist</em> in the system.</p>
11+
</li>
12+
<li>
13+
<p><code>void edit(int taskId, int newPriority)</code> updates the priority of the existing <code>taskId</code> to <code>newPriority</code>. It is <strong>guaranteed</strong> that <code>taskId</code> <em>exists</em> in the system.</p>
14+
</li>
15+
<li>
16+
<p><code>void rmv(int taskId)</code> removes the task identified by <code>taskId</code> from the system. It is <strong>guaranteed</strong> that <code>taskId</code> <em>exists</em> in the system.</p>
17+
</li>
18+
<li>
19+
<p><code>int execTop()</code> executes the task with the <strong>highest</strong> priority across all users. If there are multiple tasks with the same <strong>highest</strong> priority, execute the one with the highest <code>taskId</code>. After executing, the<strong> </strong><code>taskId</code><strong> </strong>is <strong>removed</strong> from the system. Return the <code>userId</code> associated with the executed task. If no tasks are available, return -1.</p>
20+
</li>
21+
</ul>
22+
23+
<p><strong>Note</strong> that a user may be assigned multiple tasks.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong><br />
30+
<span class="example-io">[&quot;TaskManager&quot;, &quot;add&quot;, &quot;edit&quot;, &quot;execTop&quot;, &quot;rmv&quot;, &quot;add&quot;, &quot;execTop&quot;]<br />
31+
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]</span></p>
32+
33+
<p><strong>Output:</strong><br />
34+
<span class="example-io">[null, null, null, 3, null, null, 5] </span></p>
35+
36+
<p><strong>Explanation</strong></p>
37+
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.<br />
38+
taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.<br />
39+
taskManager.edit(102, 8); // Updates priority of task 102 to 8.<br />
40+
taskManager.execTop(); // return 3. Executes task 103 for User 3.<br />
41+
taskManager.rmv(101); // Removes task 101 from the system.<br />
42+
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.<br />
43+
taskManager.execTop(); // return 5. Executes task 105 for User 5.</div>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>0 &lt;= userId &lt;= 10<sup>5</sup></code></li>
51+
<li><code>0 &lt;= taskId &lt;= 10<sup>5</sup></code></li>
52+
<li><code>0 &lt;= priority &lt;= 10<sup>9</sup></code></li>
53+
<li><code>0 &lt;= newPriority &lt;= 10<sup>9</sup></code></li>
54+
<li>At most <code>2 * 10<sup>5</sup></code> calls will be made in <strong>total</strong> to <code>add</code>, <code>edit</code>, <code>rmv</code>, and <code>execTop</code> methods.</li>
55+
<li>The input is generated such that <code>taskId</code> will be valid.</li>
56+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class TaskManager(object):
2+
def __init__(self, tasks):
3+
self.heap = []
4+
self.taskPriority = {}
5+
self.taskOwner = {}
6+
for t in tasks:
7+
self.add(t[0], t[1], t[2])
8+
9+
def add(self, userId, taskId, priority):
10+
heapq.heappush(self.heap, (-priority, -taskId))
11+
self.taskPriority[taskId] = priority
12+
self.taskOwner[taskId] = userId
13+
14+
def edit(self, taskId, newPriority):
15+
heapq.heappush(self.heap, (-newPriority, -taskId))
16+
self.taskPriority[taskId] = newPriority
17+
18+
def rmv(self, taskId):
19+
self.taskPriority[taskId] = -1
20+
21+
def execTop(self):
22+
while self.heap:
23+
negp, negid = heapq.heappop(self.heap)
24+
p = -negp
25+
tid = -negid
26+
if self.taskPriority.get(tid, -2) == p:
27+
self.taskPriority[tid] = -1
28+
return self.taskOwner.get(tid, -1)
29+
return -1
30+
31+
# Your TaskManager object will be instantiated and called as such:
32+
# obj = TaskManager(tasks)
33+
# obj.add(userId,taskId,priority)
34+
# obj.edit(taskId,newPriority)
35+
# obj.rmv(taskId)
36+
# param_4 = obj.execTop()

0 commit comments

Comments
 (0)