- 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
- 62 lines of Java from the credited upstream file 3408.java.
- 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.
1class Task implements Comparable<Task> {2 public int userId;3 public int taskId;4 public int priority;5 6 public Task() {}7 8 public Task(int userId, int taskId, int priority) {9 this.userId = userId;10 this.taskId = taskId;11 this.priority = priority;12 }13 14 @Override15 public int compareTo(Task other) {16 return this.priority == other.priority ? Integer.compare(other.taskId, this.taskId)17 : Integer.compare(other.priority, this.priority);18 }19}20 21class TaskManager {22 23 public TaskManager(List<List<Integer>> tasks) {24 taskMap = new HashMap<>();25 taskSet = new TreeSet<>();26 for (List<Integer> task : tasks)27 add(task.get(0), task.get(1), task.get(2));28 }29 30 public void add(int userId, int taskId, int priority) {31 Task task = new Task(userId, taskId, priority);32 taskMap.put(taskId, task);33 taskSet.add(task);34 }35 36 public void edit(int taskId, int newPriority) {37 Task task = taskMap.get(taskId);38 taskSet.remove(task);39 Task editedTask = new Task(task.userId, taskId, newPriority);40 taskSet.add(editedTask);41 taskMap.put(taskId, editedTask);42 }43 44 public void rmv(int taskId) {45 Task task = taskMap.get(taskId);46 taskSet.remove(task);47 taskMap.remove(taskId);48 }49 50 public int execTop() {51 if (taskSet.isEmpty())52 return -1;53 Task task = taskSet.iterator().next();54 taskSet.remove(task);55 taskMap.remove(task.taskId);56 return task.userId;57 }58 59 private Map<Integer, Task> taskMap = new HashMap<>(); 60 private Set<Task> taskSet; 61}62