3408. Design Task Manager
Description
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 specifiedtaskId
andpriority
to the user withuserId
. It is guaranteed thattaskId
does not exist in the system. -
void edit(int taskId, int newPriority)
updates the priority of the existingtaskId
tonewPriority
. It is guaranteed thattaskId
exists in the system. -
void rmv(int taskId)
removes the task identified bytaskId
from the system. It is guaranteed thattaskId
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 highesttaskId
. After executing, thetaskId
is removed from the system. Return theuserId
associated 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 <= 105
0 <= userId <= 105
0 <= taskId <= 105
0 <= priority <= 109
0 <= newPriority <= 109
- At most
2 * 105
calls will be made in total toadd
,edit
,rmv
, andexecTop
methods. - The input is generated such that
taskId
will be valid.
Solutions
Solution 1
Python3
class TaskManager:
def __init__(self, tasks: List[List[int]]):
self.d = {}
self.st = SortedList()
for task in tasks:
self.add(*task)
def add(self, userId: int, taskId: int, priority: int) -> None:
self.d[taskId] = (userId, priority)
self.st.add((-priority, -taskId))
def edit(self, taskId: int, newPriority: int) -> None:
userId, priority = self.d[taskId]
self.st.discard((-priority, -taskId))
self.d[taskId] = (userId, newPriority)
self.st.add((-newPriority, -taskId))
def rmv(self, taskId: int) -> None:
_, priority = self.d[taskId]
self.d.pop(taskId)
self.st.remove((-priority, -taskId))
def execTop(self) -> int:
if not self.st:
return -1
taskId = -self.st.pop(0)[1]
userId, _ = self.d[taskId]
self.d.pop(taskId)
return userId
# 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)
# param_4 = obj.execTop()
Java
class TaskManager {
private final Map<Integer, int[]> d = new HashMap<>();
private final TreeSet<int[]> st = new TreeSet<>((a, b) -> {
if (a[0] == b[0]) {
return b[1] - a[1];
}
return b[0] - a[0];
});
public TaskManager(List<List<Integer>> tasks) {
for (var task : tasks) {
add(task.get(0), task.get(1), task.get(2));
}
}
public void add(int userId, int taskId, int priority) {
d.put(taskId, new int[] {userId, priority});
st.add(new int[] {priority, taskId});
}
public void edit(int taskId, int newPriority) {
var e = d.get(taskId);
int userId = e[0], priority = e[1];
st.remove(new int[] {priority, taskId});
st.add(new int[] {newPriority, taskId});
d.put(taskId, new int[] {userId, newPriority});
}
public void rmv(int taskId) {
var e = d.remove(taskId);
int priority = e[1];
st.remove(new int[] {priority, taskId});
}
public int execTop() {
if (st.isEmpty()) {
return -1;
}
var e = st.pollFirst();
var t = d.remove(e[1]);
return t[0];
}
}
/**
* 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();
*/
C++
class TaskManager {
private:
unordered_map<int, pair<int, int>> d;
set<pair<int, int>> st;
public:
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) {
d[taskId] = {userId, priority};
st.insert({-priority, -taskId});
}
void edit(int taskId, int newPriority) {
auto [userId, priority] = d[taskId];
st.erase({-priority, -taskId});
st.insert({-newPriority, -taskId});
d[taskId] = {userId, newPriority};
}
void rmv(int taskId) {
auto [userId, priority] = d[taskId];
st.erase({-priority, -taskId});
d.erase(taskId);
}
int execTop() {
if (st.empty()) {
return -1;
}
auto e = *st.begin();
st.erase(st.begin());
int taskId = -e.second;
int userId = d[taskId].first;
d.erase(taskId);
return 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();
*/
Go