Created
September 18, 2025 06:50
-
-
Save SuryaPratapK/d5a82ed0d084aaeb52464177d46c7724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TaskManager { | |
| using pii = pair<int,int>; | |
| priority_queue<pii> maxheap; | |
| unordered_map<int,pii> tid_priority_uid; | |
| public: | |
| TaskManager(vector<vector<int>>& tasks) { | |
| for(auto& task: tasks){ | |
| maxheap.push({task[2],task[1]}); | |
| tid_priority_uid[task[1]] = {task[2],task[0]}; | |
| } | |
| } | |
| void add(int userId, int taskId, int priority) { | |
| maxheap.push({priority,taskId}); | |
| tid_priority_uid[taskId] = {priority,userId}; | |
| } | |
| void edit(int taskId, int newPriority) { | |
| maxheap.push({newPriority,taskId}); | |
| tid_priority_uid[taskId].first = newPriority; | |
| } | |
| void rmv(int taskId) { | |
| tid_priority_uid.erase(taskId); | |
| } | |
| int execTop() { | |
| while(!maxheap.empty()){ | |
| pii curr = maxheap.top(); | |
| maxheap.pop(); | |
| if(tid_priority_uid.count(curr.second) and tid_priority_uid[curr.second].first==curr.first){ | |
| int taskId = tid_priority_uid[curr.second].second; | |
| tid_priority_uid.erase(curr.second); | |
| return taskId; | |
| } | |
| } | |
| return -1; | |
| } | |
| }; | |
| /** | |
| * 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(); | |
| */ | |
| /* | |
| //JAVA | |
| import java.util.*; | |
| public class TaskManager { | |
| // heap stores int[]{priority, taskId} | |
| private PriorityQueue<int[]> maxheap; | |
| // map: taskId -> int[]{priority, userId} | |
| private Map<Integer, int[]> tidPriorityUid; | |
| public TaskManager(List<List<Integer>> tasks) { | |
| maxheap = new PriorityQueue<>((a, b) -> { | |
| // higher priority first, if tie smaller taskId first | |
| if (a[0] != b[0]) return Integer.compare(b[0], a[0]); | |
| return Integer.compare(a[1], b[1]); | |
| }); | |
| tidPriorityUid = new HashMap<>(); | |
| for (List<Integer> t : tasks) { | |
| int user = t.get(0), id = t.get(1), pr = t.get(2); | |
| maxheap.offer(new int[]{pr, id}); | |
| tidPriorityUid.put(id, new int[]{pr, user}); | |
| } | |
| } | |
| public void add(int userId, int taskId, int priority) { | |
| maxheap.offer(new int[]{priority, taskId}); | |
| tidPriorityUid.put(taskId, new int[]{priority, userId}); | |
| } | |
| public void edit(int taskId, int newPriority) { | |
| int[] rec = tidPriorityUid.get(taskId); | |
| if (rec == null) return; // ignore if not present | |
| rec[0] = newPriority; // update stored priority | |
| maxheap.offer(new int[]{newPriority, taskId}); // lazy update to heap | |
| } | |
| public void rmv(int taskId) { | |
| tidPriorityUid.remove(taskId); // lazy deletion | |
| } | |
| // returns taskId of executed task, or -1 if none | |
| public int execTop() { | |
| while (!maxheap.isEmpty()) { | |
| int[] cur = maxheap.poll(); | |
| int pr = cur[0], id = cur[1]; | |
| int[] rec = tidPriorityUid.get(id); | |
| if (rec != null && rec[0] == pr) { | |
| // matched current valid record -> execute and remove | |
| tidPriorityUid.remove(id); | |
| return id; | |
| } | |
| // else stale entry -> skip | |
| } | |
| return -1; | |
| } | |
| } | |
| #Python | |
| import heapq | |
| from typing import List | |
| class TaskManager: | |
| # tasks: List[List[int]] where each task = [userId, taskId, priority] | |
| def __init__(self, tasks: List[List[int]]): | |
| # heap entries: (-priority, taskId) so highest priority becomes smallest negative | |
| self.heap = [] | |
| # map: taskId -> (priority, userId) | |
| self.tid_priority_uid = {} | |
| for user, tid, pr in tasks: | |
| heapq.heappush(self.heap, (-pr, tid)) | |
| self.tid_priority_uid[tid] = (pr, user) | |
| def add(self, userId: int, taskId: int, priority: int) -> None: | |
| heapq.heappush(self.heap, (-priority, taskId)) | |
| self.tid_priority_uid[taskId] = (priority, userId) | |
| def edit(self, taskId: int, newPriority: int) -> None: | |
| rec = self.tid_priority_uid.get(taskId) | |
| if rec is None: | |
| return # ignore if not present | |
| # update map then push new entry (lazy-update) | |
| self.tid_priority_uid[taskId] = (newPriority, rec[1]) | |
| heapq.heappush(self.heap, (-newPriority, taskId)) | |
| def rmv(self, taskId: int) -> None: | |
| self.tid_priority_uid.pop(taskId, None) # lazy deletion | |
| def execTop(self) -> int: | |
| # returns taskId or -1 | |
| while self.heap: | |
| neg_pr, tid = heapq.heappop(self.heap) | |
| pr = -neg_pr | |
| rec = self.tid_priority_uid.get(tid) | |
| if rec is not None and rec[0] == pr: | |
| # valid current record: remove and return taskId | |
| self.tid_priority_uid.pop(tid, None) | |
| return tid | |
| # else stale entry -> skip | |
| return -1 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment