LeetCode 3408: Design Task Manager Solution
Master LeetCode problem 3408 (Design Task Manager), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3408. Design Task Manager
Problem Explanation
Explanation
To solve this problem, we can use a priority queue to efficiently retrieve and execute tasks with the highest priority. We can also maintain a mapping of taskId to its respective userId for quick retrieval during task execution.
- Initialize the TaskManager class with a vector of user-task-priority triples.
- Implement the add method to add a new task for a given user.
- Implement the edit method to update the priority of an existing task.
- Implement the rmv method to remove a task from the system.
- Implement the execTop method to execute the task with the highest priority and return the userId associated with that task.
Time Complexity
- Initialization: O(nlogn) where n is the number of tasks initially provided.
- add: O(logn) to add a task.
- edit: O(n) to find and update the priority of the task.
- rmv: O(n) to remove the task.
- execTop: O(logn) to execute the top task.
Space Complexity
- O(n) where n is the number of tasks initially provided.
Solution Code
import java.util.*;
class TaskManager {
private Map<Integer, Integer> taskIdToUserId;
private PriorityQueue<int[]> tasksQueue;
public TaskManager(int[][] tasks) {
taskIdToUserId = new HashMap<>();
tasksQueue = new PriorityQueue<>((a, b) -> a[2] != b[2] ? b[2] - a[2] : b[1] - a[1]);
for (int[] task : tasks) {
tasksQueue.offer(task);
taskIdToUserId.put(task[1], task[0]);
}
}
public void add(int userId, int taskId, int priority) {
int[] newTask = new int[]{userId, taskId, priority};
tasksQueue.offer(newTask);
taskIdToUserId.put(taskId, userId);
}
public void edit(int taskId, int newPriority) {
int userId = taskIdToUserId.get(taskId);
tasksQueue.removeIf(task -> task[1] == taskId);
tasksQueue.offer(new int[]{userId, taskId, newPriority});
}
public void rmv(int taskId) {
tasksQueue.removeIf(task -> task[1] == taskId);
}
public int execTop() {
if (tasksQueue.isEmpty()) {
return -1;
}
int[] task = tasksQueue.poll();
taskIdToUserId.remove(task[1]);
return task[0];
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3408 (Design Task Manager)?
This page provides optimized solutions for LeetCode problem 3408 (Design Task Manager) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3408 (Design Task Manager)?
The time complexity for LeetCode 3408 (Design Task Manager) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3408 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3408 in Java, C++, or Python.