1834. Single-Threaded CPU
You are given n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where tasks[i] = [enqueueTimei, processingTimei] means that the ith task will be available to process at enqueueTimei and will take processingTimei to finish processing.
You have a single-threaded CPU that can process at most one task at a time and will act in the following way:
- If the CPU is idle and there are no available tasks to process, the CPU remains idle.
 - If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
 - Once a task is started, the CPU will process the entire task without stopping.
 - The CPU can finish a task then start a new one instantly.
 
Return the order in which the CPU will process the tasks.
Example: 1
Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
Output: [0,2,3,1]
Example: 2
Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
Output: [4,3,2,0,1]
- tasks.length == n
 - 1 <= n <= 10 ^ 5
 - 1 <= enqueueTimei, processingTimei <= 10 ^ 9
 
- First of all we should see to it that if enqueue time of a task less than or equal to currentTime, then, all these tasks will be added into our available list.
 - So we have to choose from the available list, some tasks that have less amount of required completion time.
 - Also if many tasks have same completion time we will have to take the task with least index.
 - Since we will be adding task to our available list at anytime, we use a priority queue so that when the task is added, we need not sort the array once again. Add operation in priority queue only takes logN time.
 
- Since we are to consider 3 factors, i.e enqueueTime, requiredTime and index, we will add index to every tasks[i].
 - Also we will sort the tasks array based on it's enqueueTime, requiredTime and index respectively. We do this so that we can get all available tasks based on its enqueueTime.
 
- Consider the case [[20, 20], [50, 50]].
 - Here after the 0th task gets completed, we will not be having any task in our queue.
 - So we will ensure that, if our queue is empty, we will add next available task from our tasks list.
 
Comments
Post a Comment
If you have any queries or need solution for any problem, please let me know.