|
| 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<vector<int>>& 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> </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">["TaskManager", "add", "edit", "execTop", "rmv", "add", "execTop"]<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> </p> |
| 46 | +<p><strong>Constraints:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= tasks.length <= 10<sup>5</sup></code></li> |
| 50 | + <li><code>0 <= userId <= 10<sup>5</sup></code></li> |
| 51 | + <li><code>0 <= taskId <= 10<sup>5</sup></code></li> |
| 52 | + <li><code>0 <= priority <= 10<sup>9</sup></code></li> |
| 53 | + <li><code>0 <= newPriority <= 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> |
0 commit comments