3408. Design Task Manager LeetCode Solution

In this guide, you will get 3408. Design Task Manager LeetCode Solution with the best time and space complexity. The solution to Design Task Manager problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Design Task Manager solution in C++
  4. Design Task Manager solution in Java
  5. Design Task Manager solution in Python
  6. Additional Resources
3408. Design Task Manager LeetCode Solution image

Problem Statement of Design Task Manager

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 TaskManager class:

TaskManager(vector<vector>& tasks) initializes the task manager with a list of user-task-priority triples. Each element in the input list is of the form [userId, taskId, priority], which adds a task to the specified user with the given priority.

void add(int userId, int taskId, int priority) adds a task with the specified taskId and priority to the user with userId. It is guaranteed that taskId does not exist in the system.

void edit(int taskId, int newPriority) updates the priority of the existing taskId to newPriority. It is guaranteed that taskId exists in the system.

void rmv(int taskId) removes the task identified by taskId from the system. It is guaranteed that taskId exists in the system.

int execTop() executes the task with the highest priority across all users. If there are multiple tasks with the same highest priority, execute the one with the highest taskId. After executing, the taskId is removed from the system. Return the userId associated with the executed task. If no tasks are available, return -1.

See also  1531. String Compression II LeetCode Solution

Note that a user may be assigned multiple tasks.

Example 1:

Input:
[“TaskManager”, “add”, “edit”, “execTop”, “rmv”, “add”, “execTop”]
[[[[1, 101, 10], [2, 102, 20], [3, 103, 15]]], [4, 104, 5], [102, 8], [], [101], [5, 105, 15], []]
Output:
[null, null, null, 3, null, null, 5]
Explanation
TaskManager taskManager = new TaskManager([[1, 101, 10], [2, 102, 20], [3, 103, 15]]); // Initializes with three tasks for Users 1, 2, and 3.
taskManager.add(4, 104, 5); // Adds task 104 with priority 5 for User 4.
taskManager.edit(102, 8); // Updates priority of task 102 to 8.
taskManager.execTop(); // return 3. Executes task 103 for User 3.
taskManager.rmv(101); // Removes task 101 from the system.
taskManager.add(5, 105, 15); // Adds task 105 with priority 15 for User 5.
taskManager.execTop(); // return 5. Executes task 105 for User 5.

Constraints:

1 <= tasks.length <= 105
0 <= userId <= 105
0 <= taskId <= 105
0 <= priority <= 109
0 <= newPriority <= 109
At most 2 * 105 calls will be made in total to add, edit, rmv, and execTop methods.
The input is generated such that taskId will be valid.

Complexity Analysis

  • Time Complexity: O(\log n)
  • Space Complexity: O(|\texttt{tasks}|)

3408. Design Task Manager LeetCode Solution in C++

struct Task {
  int userId;
  int taskId;
  int priority;

  Task() = default;
  Task(int u, int t, int p) : userId(u), taskId(t), priority(p) {}

  bool operator<(const Task& other) const {
    return priority == other.priority ? taskId > other.taskId
                                      : priority > other.priority;
  }
};

class TaskManager {
 public:
  unordered_map<int, Task> taskMap;  // {taskId: Task}
  set<Task> taskSet;  // Stores tasks sorted by priority and taskId.

  TaskManager(vector<vector<int>>& tasks) {
    for (const auto& task : tasks)
      add(task[0], task[1], task[2]);
  }

  void add(int userId, int taskId, int priority) {
    const Task task(userId, taskId, priority);
    taskMap[taskId] = task;
    taskSet.insert(task);
  }

  void edit(int taskId, int newPriority) {
    const Task task = taskMap[taskId];
    taskSet.erase(task);
    const Task editedTask = Task(task.userId, task.taskId, newPriority);
    taskSet.insert(editedTask);
    taskMap[taskId] = editedTask;
  }

  void rmv(int taskId) {
    const Task task = taskMap[taskId];
    taskSet.erase(task);
    taskMap.erase(taskId);
  }

  int execTop() {
    if (taskSet.empty())
      return -1;
    const Task task = *taskSet.begin();
    taskSet.erase(task);
    taskMap.erase(task.taskId);
    return task.userId;
  }
};
/* code provided by PROGIEZ */

3408. Design Task Manager LeetCode Solution in Java

class Task implements Comparable<Task> {
  public int userId;
  public int taskId;
  public int priority;

  public Task() {}

  public Task(int userId, int taskId, int priority) {
    this.userId = userId;
    this.taskId = taskId;
    this.priority = priority;
  }

  @Override
  public int compareTo(Task other) {
    return this.priority == other.priority ? Integer.compare(other.taskId, this.taskId)
                                           : Integer.compare(other.priority, this.priority);
  }
}

class TaskManager {

  public TaskManager(List<List<Integer>> tasks) {
    taskMap = new HashMap<>();
    taskSet = new TreeSet<>();
    for (List<Integer> task : tasks)
      add(task.get(0), task.get(1), task.get(2));
  }

  public void add(int userId, int taskId, int priority) {
    Task task = new Task(userId, taskId, priority);
    taskMap.put(taskId, task);
    taskSet.add(task);
  }

  public void edit(int taskId, int newPriority) {
    Task task = taskMap.get(taskId);
    taskSet.remove(task);
    Task editedTask = new Task(task.userId, taskId, newPriority);
    taskSet.add(editedTask);
    taskMap.put(taskId, editedTask);
  }

  public void rmv(int taskId) {
    Task task = taskMap.get(taskId);
    taskSet.remove(task);
    taskMap.remove(taskId);
  }

  public int execTop() {
    if (taskSet.isEmpty())
      return -1;
    Task task = taskSet.iterator().next();
    taskSet.remove(task);
    taskMap.remove(task.taskId);
    return task.userId;
  }

  private Map<Integer, Task> taskMap = new HashMap<>(); // {taskId: Task}
  private Set<Task> taskSet; // Stores tasks sorted by priority and taskId.
}
// code provided by PROGIEZ

3408. Design Task Manager LeetCode Solution in Python

from dataclasses import dataclass
from sortedcontainers import SortedDict, SortedSet


@dataclass(frozen=True)
class Task:
  userId: int
  taskId: int
  priority: int

  def __lt__(self, other):
    if self.priority == other.priority:
      return self.taskId > other.taskId
    return self.priority > other.priority


class TaskManager:
  def __init__(self, tasks: list[list[int]]):
    self.taskMap = SortedDict()  # {taskId: Task}, keeps tasks sorted by taskId
    self.taskSet = SortedSet()  # Stores tasks sorted by priority and taskId
    for task in tasks:
      self.add(task[0], task[1], task[2])

  def add(self, userId: int, taskId: int, priority: int) -> None:
    task = Task(userId, taskId, priority)
    self.taskMap[taskId] = task
    self.taskSet.add(task)

  def edit(self, taskId: int, newPriority: int) -> None:
    task = self.taskMap[taskId]
    self.taskSet.remove(task)
    editedTask = Task(task.userId, taskId, newPriority)
    self.taskSet.add(editedTask)
    self.taskMap[taskId] = editedTask

  def rmv(self, taskId: int) -> None:
    task = self.taskMap[taskId]
    self.taskSet.remove(task)
    del self.taskMap[taskId]

  def execTop(self):
    if not self.taskSet:
      return -1
    task = self.taskSet.pop(0)
    del self.taskMap[task.taskId]
    return task.userId
# code by PROGIEZ

Additional Resources

See also  797. All Paths From Source to Target LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.