I have a system that stores a huge number of tasks. Each task has the following parameters:
Time t
Priority p
Every task has a unique time. That means no 2 tasks can have the same Time parameter.
However, Priority is not unique. Multiple tasks can have the same priorities.
I need to design a system which can address four types of queries. The probability of each type of query is equally likely. Following are the types of queries:
UpdateTask(t,p)
This query wants the system to set the priority of task at timet
to priorityp
. If the task does not exist in the system, a fresh task is created. If the task is present, its priority is updated.DeleteTask(t)
This query wants the system to delete a task that is associated with timet
. If such a task is not present in the system, then no action needs to be taken.GetMaximumPriority() and GetMinimumPriority()
This query wants the system to print the minimum priority and maximum priority of the tasks available in the system.GetPriorityofTaskAtMaximumTime()
This query wants the system to print the priority of the task that has the maximum value of parametert
(time)
I need to design the data structure for this system and implement algorithms for those data structures. I need to implement this in Java.
My approach: Created a HashMap
with Key as Time and Value as Priority. The HashMap
allows me to address the first two queries in constant time. But the last two queries have a time complexity of O(n)
.
Question: Is there a better time efficient and space efficient data structures and algorithms for this problem? I mainly need an approach to solve this. Fully implemented code is not necessary. Thanks.