2025-09-18 Daily Challenge
Today I have done leetcode's September LeetCoding Challenge with cpp.
September LeetCoding Challenge 18
Description
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<int>>& 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 specifiedtaskIdandpriorityto the user withuserId. It is guaranteed thattaskIddoes not exist in the system. -
void edit(int taskId, int newPriority)updates the priority of the existingtaskIdtonewPriority. It is guaranteed thattaskIdexists in the system. -
void rmv(int taskId)removes the task identified bytaskIdfrom the system. It is guaranteed thattaskIdexists 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 highesttaskId. After executing, thetaskIdis removed from the system. Return theuserIdassociated with the executed task. If no tasks are available, return -1.
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 <= 1050 <= userId <= 1050 <= taskId <= 1050 <= priority <= 1090 <= newPriority <= 109- At most
2 * 105calls will be made in total toadd,edit,rmv, andexecTopmethods. - The input is generated such that
taskIdwill be valid.
Solution
class TaskManager {
map<int, set<int, greater<int>>> priority2tasks;
map<int, int> task2priority;
map<int, int> task2user;
void removeOldPriority(int taskId) {
int oldPriority = task2priority[taskId];
priority2tasks[oldPriority].erase(taskId);
if(priority2tasks[oldPriority].empty()) {
priority2tasks.erase(oldPriority);
}
}
public:
TaskManager(vector<vector<int>>& tasks) {
for(const auto &t : tasks) {
task2priority[t[1]] = t[2];
priority2tasks[t[2]].insert(t[1]);
task2user[t[1]] = t[0];
}
}
void add(int userId, int taskId, int priority) {
task2priority[taskId] = priority;
priority2tasks[priority].insert(taskId);
task2user[taskId] = userId;
}
void edit(int taskId, int newPriority) {
removeOldPriority(taskId);
task2priority[taskId] = newPriority;
priority2tasks[newPriority].insert(taskId);
}
void rmv(int taskId) {
removeOldPriority(taskId);
task2priority.erase(taskId);
task2user.erase(taskId);
}
int execTop() {
if(task2user.empty()) return -1;
int taskId = *(priority2tasks.rbegin()->second.begin());
int userId = task2user[taskId];
removeOldPriority(taskId);
task2priority.erase(taskId);
task2user.erase(taskId);
return userId;
}
};