Priority-Based Task Manager | Optimized Solution| Beats 100%
Intuition
The task manager tracks tasks with unique task IDs, their associated user IDs, and their priorities.
It uses a hashmap to store task details and a pri
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
1. The task manager tracks tasks with unique task IDs, their associated user IDs, and their priorities.
2. It uses a hashmap to store task details and a priority queue to fetch the highest-priority task efficiently.
3. Tasks can be added, edited, removed, and executed with efficient handling of priorities.

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

* A hashmap (unmap) stores task details: {taskId: {userId, priority}}.
* A max heap (priority queue) maintains tasks in descending order of priority.
2. Adding Tasks:

* Update the hashmap with the new task details.
* Push the task into the priority queue.
Editing Tasks:

3. Update the priority in the hashmap.
* Push the updated task into the priority queue to maintain heap properties.
Removing Tasks:

4. Remove the task from the hashmap.
* The priority queue will eventually discard stale data when execTop is called.
5. Executing Top Priority Task:

* Pop tasks from the queue until the top task matches the hashmap's current priority.
* Execute the task by removing it from the hashmap and returning the associated user ID.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
1. Add/Edit: 
O(logN) for priority queue update.
2. Remove: 
O(1) for hashmap erase.
3. Execute Top: 
O(logN) amortized for removing stale tasks.

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
* O(N) for the hashmap and priority queue.

# Code
```cpp []
#include <unordered_map>
#include <queue>
#include <vector>
#include <climits>
using namespace std;

class TaskManager {
    unordered_map<int, pair<int, int>> unmap; 
    priority_queue<pair<int, int>> pq;
    

public:
    TaskManager(vector<vector<int>>& tasks) {
        for (auto& it : tasks) {
            unmap[it[1]] = {it[0], it[2]};
            pq.push({it[2], it[1]});
        }
    }
    
    void add(int userId, int taskId, int priority) {
        unmap[taskId] = {userId, priority};
        pq.push({priority, taskId});
    }
    
    void edit(int taskId, int newPriority) {
        if (unmap.find(taskId) != unmap.end()) {
            unmap[taskId].second = newPriority;
            pq.push({newPriority, taskId});
        }
    }
    
    void rmv(int taskId) {
        unmap.erase(taskId);
    }
    
    int execTop() {
    while (!pq.empty()) {
        int taskId = pq.top().second;
        if (unmap.find(taskId) == unmap.end()) {
            pq.pop();
        }
        else if (unmap[taskId].second != pq.top().first) {
            pq.pop();
        }
        else {
           
            int user = unmap[taskId].first;
            unmap.erase(taskId); 
            pq.pop();
            return user;
        }
    }
    return -1; 
}

};

```
['Ordered Map', 'Heap (Priority Queue)', 'C++']