Efficient CPP solution using a map
IntuitionThe crux of this problem is to access the userid and priority of task using the taskid and not the other way. This was we can eleminate redundant itera
# Intuition
The crux of this problem is to access the userid and priority of task using the taskid and not the other way. This was we can eleminate redundant iterations over the map.
1. to edit the priority or
2. to fetch the userid based on taskid.

To get the toptasks efficiently, we maintain a max heap using priority queue data structure.

# Approach
Code is pretty much self explanatory.

# Code
```cpp []
class TaskManager {
private:
    unordered_map<int, pair<int, int>> mp;              // taskId -> (userId, priority)
    priority_queue<tuple<int, int, int>> pq;           // (priority, taskId, userId)

    void updateTop(int u, int p, int t) {
        pq.push({p, t, u});
    }

public:
    TaskManager(vector<vector<int>>& tasks) {
        for (auto& task : tasks) {
            int u = task[0];
            int t = task[1];
            int p = task[2];

            mp[t] = {u, p};
            updateTop(u, p, t);
        }
    }
    
    void add(int u, int t, int p) {
        mp[t] = {u, p};
        updateTop(u, p, t);
    }
    
    void edit(int taskId, int newPrio) {
        if (mp.find(taskId) == mp.end()) 
            return;

        auto [u, oldPrio] = mp[taskId];
        mp.erase(taskId);

        if (newPrio != -1) {
            mp[taskId] = {u, newPrio};
            updateTop(u, newPrio, taskId);
        }
    }
    
    void rmv(int taskId) {
        edit(taskId, -1);
    }
    
    int execTop() {
        while (!pq.empty()) {
            auto [p, t, u] = pq.top();
            pq.pop();

            if (mp.find(t) != mp.end() && mp[t] == make_pair(u, p)) {
                rmv(t);
                return u;
            }
        }

        return -1;
    }
};

```
['Hash Table', 'Design', 'Heap (Priority Queue)', 'C++']