- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 54 lines of C++ from the credited upstream file 3408.cpp.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- 1 loop block detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1struct Task {2 int userId;3 int taskId;4 int priority;5 6 Task() = default;7 Task(int u, int t, int p) : userId(u), taskId(t), priority(p) {}8 9 bool operator<(const Task& other) const {10 return priority == other.priority ? taskId > other.taskId11 : priority > other.priority;12 }13};14 15class TaskManager {16 public:17 unordered_map<int, Task> taskMap; 18 set<Task> taskSet; 19 20 TaskManager(vector<vector<int>>& tasks) {21 for (const auto& task : tasks)22 add(task[0], task[1], task[2]);23 }24 25 void add(int userId, int taskId, int priority) {26 const Task task(userId, taskId, priority);27 taskMap[taskId] = task;28 taskSet.insert(task);29 }30 31 void edit(int taskId, int newPriority) {32 const Task task = taskMap[taskId];33 taskSet.erase(task);34 const Task editedTask = Task(task.userId, task.taskId, newPriority);35 taskSet.insert(editedTask);36 taskMap[taskId] = editedTask;37 }38 39 void rmv(int taskId) {40 const Task task = taskMap[taskId];41 taskSet.erase(task);42 taskMap.erase(taskId);43 }44 45 int execTop() {46 if (taskSet.empty())47 return -1;48 const Task task = *taskSet.begin();49 taskSet.erase(task);50 taskMap.erase(task.taskId);51 return task.userId;52 }53};54