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 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 taskIdexists in the system.
void rmv(int taskId) removes the task identified by taskId from the system. It is guaranteed that taskIdexists 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, thetaskIdis removed from the system. Return the userId associated with the executed task. If no tasks are available, return -1.
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.
fromsortedcontainersimportSortedListclassTaskManager:def__init__(self,tasks:List[List[int]]):self.d={}self.st=SortedList()fortaskintasks:self.add(*task)defadd(self,userId:int,taskId:int,priority:int)->None:self.d[taskId]=(userId,priority)self.st.add((-priority,-taskId))defedit(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))defrmv(self,taskId:int)->None:_,priority=self.d[taskId]self.d.pop(taskId)self.st.remove((-priority,-taskId))defexecTop(self)->int:ifnotself.st:return-1taskId=-self.st.pop(0)[1]userId,_=self.d[taskId]self.d.pop(taskId)returnuserId# 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()
classTaskManager{privatefinalMap<Integer,int[]>d=newHashMap<>();privatefinalTreeSet<int[]>st=newTreeSet<>((a,b)->{if(a[0]==b[0]){returnb[1]-a[1];}returnb[0]-a[0];});publicTaskManager(List<List<Integer>>tasks){for(vartask:tasks){add(task.get(0),task.get(1),task.get(2));}}publicvoidadd(intuserId,inttaskId,intpriority){d.put(taskId,newint[]{userId,priority});st.add(newint[]{priority,taskId});}publicvoidedit(inttaskId,intnewPriority){vare=d.get(taskId);intuserId=e[0],priority=e[1];st.remove(newint[]{priority,taskId});st.add(newint[]{newPriority,taskId});d.put(taskId,newint[]{userId,newPriority});}publicvoidrmv(inttaskId){vare=d.remove(taskId);intpriority=e[1];st.remove(newint[]{priority,taskId});}publicintexecTop(){if(st.isEmpty()){return-1;}vare=st.pollFirst();vart=d.remove(e[1]);returnt[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(); */
classTaskManager{private:unordered_map<int,pair<int,int>>d;set<pair<int,int>>st;public:TaskManager(vector<vector<int>>&tasks){for(constauto&task:tasks){add(task[0],task[1],task[2]);}}voidadd(intuserId,inttaskId,intpriority){d[taskId]={userId,priority};st.insert({-priority,-taskId});}voidedit(inttaskId,intnewPriority){auto[userId,priority]=d[taskId];st.erase({-priority,-taskId});st.insert({-newPriority,-taskId});d[taskId]={userId,newPriority};}voidrmv(inttaskId){auto[userId,priority]=d[taskId];st.erase({-priority,-taskId});d.erase(taskId);}intexecTop(){if(st.empty()){return-1;}autoe=*st.begin();st.erase(st.begin());inttaskId=-e.second;intuserId=d[taskId].first;d.erase(taskId);returnuserId;}};/** * 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(); */