← All problems

3408. Design Task Manager

MediumOpen on LeetCodeProblem statement

Problem Statement

3408. Design Task Manager

Medium


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:

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:

Java

Source file
public class Task {
    int userId;
    int taskId;
    int priority;

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

}

class TaskManager {
    HashMap<Integer, Task> taskMap;
    TreeSet<Task> taskList;

    public TaskManager(List<List<Integer>> tasks) {
        taskMap = new HashMap<>();
        taskList = new TreeSet<>(
                (a, b) -> a.priority == b.priority ? b.taskId - a.taskId : b.priority - a.priority);

        for (List<Integer> t : tasks) {
            Task newTask = new Task(t.get(0), t.get(1), t.get(2));
            taskList.add(newTask);
            taskMap.put(t.get(1), newTask);
        }
    }

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

    public void edit(int taskId, int newPriority) {
        Task oldTask = taskMap.get(taskId);
        taskList.remove(oldTask);
        oldTask.priority = newPriority;
        taskList.add(oldTask);
        taskMap.put(taskId, oldTask);
    }

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

    public int execTop() {
        if (taskList.isEmpty())
            return -1;
        Task topTask = taskList.first();
        taskList.remove(topTask);
        taskMap.remove(topTask.taskId);
        return topTask.userId;
    }
}

/**
 * Your TaskManager object will be instantiated and called as such:
 * TaskManager obj = new TaskManager(tasks);
 * obj.add(userId,taskId,priority);
 * obj.edit(taskId,newPriority);
 * obj.rmv(taskId);
 * int param_4 = obj.execTop();
 */