C++ solution | Beats 100% | Simple Map and Set
IntuitionTo keep track of highest priority, we need some ordered data structure to fetch highest values efficiently. Set and Map are useful here.ApproachStore t
# Intuition
To keep track of highest priority, we need some ordered data structure to fetch highest values efficiently. Set and Map are useful here.

# Approach
Store taskId with their corresponding userId and priority, also user Priority as a key in map to get the highest priority task easily (works well also in case there are multiple tasks with same priority).

# Complexity
- Time complexity:
O((log2N)^2)

- Space complexity:
O(N)

# Code
```cpp []
class TaskManager {
    unordered_map<int, pair<int, int> > taskUser; // task -> {user, prioirty}
    map<int, set<int> > taskPriority;
public:
    TaskManager(vector<vector<int>>& tasks) {
        for(auto t: tasks){
           taskUser[t[1]] = {t[0], t[2]};
           taskPriority[t[2]].insert(t[1]);
        }
    }
    
    void add(int userId, int taskId, int priority) {
       taskUser[taskId] = {userId, priority};
       taskPriority[priority].insert(taskId);
    }
    
    void edit(int taskId, int newPriority) {
        int oldPriority = taskUser[taskId].second;
        taskPriority[oldPriority].erase(taskId);
        if(taskPriority[oldPriority].empty()){
            taskPriority.erase(oldPriority);
        }
        taskUser[taskId].second = newPriority;
        taskPriority[newPriority].insert(taskId);
    }
    
    void rmv(int taskId) {
        int priority = taskUser[taskId].second;
        taskPriority[priority].erase(taskId);
        if(taskPriority[priority].empty()){
            taskPriority.erase(priority);
        }
        taskUser.erase(taskId);
    }
    
    int execTop() {
        if(taskPriority.empty())
          return -1;
        auto i = prev(taskPriority.end());
        int taskId = *prev(i->second.end());
        int userId = taskUser[taskId].first;
        rmv(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();
 */
```
['C++']