🔥Unordered Map+Set Solution for Beginners!🔥
Intuition-Retrieve the task with the highest priority quickly.
-Modify, add, and remove tasks dynamically while maintaining order. Using a combination of a std:
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->The problem revolves around efficiently managing tasks based on their priorities and task IDs. We aim to:

-Retrieve the task with the highest priority quickly.
-Modify, add, and remove tasks dynamically while maintaining order. Using a combination of a std::unordered_map and a std::set, we can achieve efficient lookups and maintain sorted order of tasks by priority


# Approach
<!-- Describe your approach to solving the problem. -->
1) Data Structures:

- std::unordered_map<int, pair<int, int>> mp:
Maps taskId to {userId, priority} for quick access.
- std::set<pair<int, int>, greater<pair<int, int>>> st:
Maintains tasks sorted by priority (descending). If priorities are equal, tasks are sorted by taskId.
Operations:

2) Initialization:
- Populate mp and st with the initial list of tasks.
- Add:
Insert a new task into both mp and st.
- Edit:
Find and remove the old priority-task pair from st.
Update the task in mp and insert the new priority-task pair into st.
- Remove:
Remove the task from both st and mp.
- Execute Top:
Access the top element of st (highest priority).
Remove it from both mp and st and return the associated userId.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
1) Initialization:O(nlogn): Inserting n elements into the set. 
2) Add:O(logn):Insertion into the set and map
3) Edit:O(logn): Removing and inserting an element in the set, plus map update.
4) Remove:O(logn):Removing from the set and map.
5) Execute top:O(logn):Accessing and removing the top element from the set.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
1) O(n):unordered_map
2) O(n):set



![image.png](https://assets.leetcode.com/users/images/3dc1449c-432c-4d0f-b027-3a8cb5afae40_1737649389.762263.png)


# Code
```cpp []
class TaskManager {
public:
    unordered_map<int,pair<int,int>>mp;
    set<pair<int,int>,greater<pair<int,int>>>st;

    TaskManager(vector<vector<int>>& tasks) {
        for(auto& it:tasks){
            mp[it[1]]={it[0],it[2]};//taskId->{userId,priority}
            st.insert({it[2],it[1]});//{priority,taskId}
        }
    }
    
    void add(int userId, int taskId, int priority) {
        mp[taskId]={userId,priority};
        st.insert({priority,taskId});
    }
    
    void edit(int taskId, int newPriority) {
        pair<int,int>element={mp[taskId].second,taskId};
        pair<int,int>newElement={newPriority,taskId};

        auto it=st.find(element);
        st.erase(it);
        st.insert(newElement);

        mp[taskId].second=newPriority;
    }
    
    void rmv(int taskId) {
        pair<int,int>element={mp[taskId].second,taskId};
        auto it=st.find(element);
        st.erase(it);

        mp.erase(taskId);
    }
    
    int execTop() {
        auto firstElement = *st.begin();
        int taskId=firstElement.second;
        
        if(mp.find(taskId)!=mp.end()){
            int ansUserId=mp[taskId].first;

            rmv(taskId);

            return ansUserId;
        }
        else 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();
 */
```
['C++']