|
| 1 | +// Source : https://leetcode.com/problems/single-threaded-cpu/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-04-20 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks, where |
| 8 | + * tasks[i] = [enqueueTimei, processingTimei] means that the i^th task will be available to |
| 9 | + * process at enqueueTimei and will take processingTimei to finish processing. |
| 10 | + * |
| 11 | + * You have a single-threaded CPU that can process at most one task at a time and will act in the |
| 12 | + * following way: |
| 13 | + * |
| 14 | + * If the CPU is idle and there are no available tasks to process, the CPU remains idle. |
| 15 | + * If the CPU is idle and there are available tasks, the CPU will choose the one with the |
| 16 | + * shortest processing time. If multiple tasks have the same shortest processing time, it will choose |
| 17 | + * the task with the smallest index. |
| 18 | + * Once a task is started, the CPU will process the entire task without stopping. |
| 19 | + * The CPU can finish a task then start a new one instantly. |
| 20 | + * |
| 21 | + * Return the order in which the CPU will process the tasks. |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: tasks = [[1,2],[2,4],[3,2],[4,1]] |
| 26 | + * Output: [0,2,3,1] |
| 27 | + * Explanation: The events go as follows: |
| 28 | + * - At time = 1, task 0 is available to process. Available tasks = {0}. |
| 29 | + * - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. |
| 30 | + * - At time = 2, task 1 is available to process. Available tasks = {1}. |
| 31 | + * - At time = 3, task 2 is available to process. Available tasks = {1, 2}. |
| 32 | + * - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. |
| 33 | + * Available tasks = {1}. |
| 34 | + * - At time = 4, task 3 is available to process. Available tasks = {1, 3}. |
| 35 | + * - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. |
| 36 | + * Available tasks = {1}. |
| 37 | + * - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. |
| 38 | + * - At time = 10, the CPU finishes task 1 and becomes idle. |
| 39 | + * |
| 40 | + * Example 2: |
| 41 | + * |
| 42 | + * Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] |
| 43 | + * Output: [4,3,2,0,1] |
| 44 | + * Explanation: The events go as follows: |
| 45 | + * - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. |
| 46 | + * - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. |
| 47 | + * - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. |
| 48 | + * - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. |
| 49 | + * - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. |
| 50 | + * - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. |
| 51 | + * - At time = 40, the CPU finishes task 1 and becomes idle. |
| 52 | + * |
| 53 | + * Constraints: |
| 54 | + * |
| 55 | + * tasks.length == n |
| 56 | + * 1 <= n <= 10^5 |
| 57 | + * 1 <= enqueueTimei, processingTimei <= 10^9 |
| 58 | + ******************************************************************************************************/ |
| 59 | + |
| 60 | +class Solution { |
| 61 | +private: |
| 62 | + template<typename T> |
| 63 | + void print(T q) { |
| 64 | + while(!q.empty()) { |
| 65 | + auto t = q.top(); |
| 66 | + cout << t[2]<< "[" << t[0] <<","<< t[1] << "] "; |
| 67 | + q.pop(); |
| 68 | + } |
| 69 | + std::cout << '\n'; |
| 70 | + } |
| 71 | +public: |
| 72 | + vector<int> getOrder(vector<vector<int>>& tasks) { |
| 73 | + // push the index into each task. |
| 74 | + // [enQueueTime, ProcessingTime, index] |
| 75 | + for(int i=0; i<tasks.size(); i++){ |
| 76 | + tasks[i].push_back(i); |
| 77 | + } |
| 78 | + |
| 79 | + //Sort the tasks by enQueueTtime |
| 80 | + sort(tasks.begin(), tasks.end(), [&](vector<int>& lhs, vector<int>& rhs) { |
| 81 | + return lhs[0] < rhs[0]; |
| 82 | + }); |
| 83 | + |
| 84 | + //Sort function for tasks priority queue. |
| 85 | + auto comp = [&](vector<int>& lhs, vector<int>& rhs){ |
| 86 | + // if the processing time is same ,get the smaller index |
| 87 | + if (lhs[1] == rhs[1]) return lhs[2] > rhs[2]; |
| 88 | + // choosing the shorter processing time. |
| 89 | + return lhs[1] > rhs[1]; |
| 90 | + }; |
| 91 | + |
| 92 | + priority_queue<vector<int>, std::vector<vector<int>>, decltype(comp)> q (comp); |
| 93 | + vector<int> result; |
| 94 | + |
| 95 | + int i = 0; |
| 96 | + while (i < tasks.size()) { |
| 97 | + long time = tasks[i][0]; |
| 98 | + int start = i; |
| 99 | + for (;i < tasks.size() && tasks[start][0] == tasks[i][0];i++ ) { |
| 100 | + q.push(tasks[i]); |
| 101 | + } |
| 102 | + //print(q); |
| 103 | + |
| 104 | + while(!q.empty()){ |
| 105 | + //processing the task |
| 106 | + auto t = q.top(); q.pop(); |
| 107 | + //cout << "DEQUEUE: " << t[2] << ":[" << t[0] <<","<< t[1] << "]"<< endl; |
| 108 | + result.push_back(t[2]); |
| 109 | + |
| 110 | + //enQueue the tasks when CPU proceing the current task |
| 111 | + time += t[1]; |
| 112 | + for(;i < tasks.size() && tasks[i][0] <= time; i++) { |
| 113 | + //cout << "ENQUEUE: " << tasks[i][2] << ":[" << tasks[i][0] <<","<< tasks[i][1] << "]"<< endl; |
| 114 | + q.push(tasks[i]); |
| 115 | + } |
| 116 | + //print(q); |
| 117 | + //cout << endl; |
| 118 | + } |
| 119 | + } |
| 120 | + return result; |
| 121 | + } |
| 122 | +}; |
0 commit comments