3408. Design Task Manager #2188
-
Topics: 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. Implement the
Note that a user may be assigned multiple tasks. Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to design a task management system that efficiently handles adding, editing, removing, and executing tasks based on their priority. The key challenge is to ensure that the system can quickly access the task with the highest priority (and highest task ID in case of ties) while supporting updates and removals without incurring high computational costs. Approach
Let's implement this solution in PHP: 3408. Design Task Manager <?php
class TaskManager {
private $taskInfo;
private $generations;
private $heap;
/**
* @param Integer[][] $tasks
*/
function __construct($tasks) {
$this->taskInfo = [];
$this->generations = [];
$this->heap = new CustomMaxHeap();
foreach ($tasks as $task) {
$this->add($task[0], $task[1], $task[2]);
}
}
/**
* @param Integer $userId
* @param Integer $taskId
* @param Integer $priority
* @return NULL
*/
function add($userId, $taskId, $priority) {
if (!isset($this->generations[$taskId])) {
$this->generations[$taskId] = 0;
} else {
$this->generations[$taskId]++;
}
$gen = $this->generations[$taskId];
$this->taskInfo[$taskId] = [$userId, $priority, $gen];
$this->heap->insert([$priority, $taskId, $userId, $gen]);
}
/**
* @param Integer $taskId
* @param Integer $newPriority
* @return NULL
*/
function edit($taskId, $newPriority) {
$this->generations[$taskId]++;
$gen = $this->generations[$taskId];
$userId = $this->taskInfo[$taskId][0];
$this->taskInfo[$taskId] = [$userId, $newPriority, $gen];
$this->heap->insert([$newPriority, $taskId, $userId, $gen]);
}
/**
* @param Integer $taskId
* @return NULL
*/
function rmv($taskId) {
unset($this->taskInfo[$taskId]);
}
/**
* @return Integer
*/
function execTop() {
while (!$this->heap->isEmpty()) {
$top = $this->heap->extract();
list($p, $taskId, $userId, $gen) = $top;
if (isset($this->taskInfo[$taskId]) && $this->taskInfo[$taskId][2] === $gen) {
unset($this->taskInfo[$taskId]);
return $userId;
}
}
return -1;
}
}
class CustomMaxHeap extends SplMaxHeap {
/**
* @param $a
* @param $b
* @return int
*/
public function compare($a, $b): int {
if ($a[0] != $b[0]) {
return $a[0] - $b[0];
}
return $a[1] - $b[1];
}
}
/**
* Your TaskManager object will be instantiated and called as such:
* $obj = TaskManager($tasks);
* $obj->add($userId, $taskId, $priority);
* $obj->edit($taskId, $newPriority);
* $obj->rmv($taskId);
* $ret_4 = $obj->execTop();
*/
// Test cases
$taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]);
$taskManager->add(4, 104, 5); // Add task 104 for user 4
$taskManager->edit(102, 8); // Change priority of 102 to 8
echo $taskManager->execTop(); // → 3 (task 103 has highest priority 15)
$taskManager->rmv(101); // Remove task 101
$taskManager->add(5, 105, 15); // Add task 105 with priority 15
echo $taskManager->execTop(); // → 5 (tie with 103, but 105 has higher taskId)
?> Explanation:
This approach efficiently manages tasks by leveraging a max heap for priority-based access and generation numbers to handle updates and removals lazily, ensuring optimal performance for each operation. |
Beta Was this translation helpful? Give feedback.
We need to design a task management system that efficiently handles adding, editing, removing, and executing tasks based on their priority. The key challenge is to ensure that the system can quickly access the task with the highest priority (and highest task ID in case of ties) while supporting updates and removals without incurring high computational costs.
Approach
Data Structures:
$taskInfo
to store task details (user ID, priority, and generation number) keyed by task ID.$generations
to track the current generation number for each task ID. This helps in invalidating outdated …