3
vector<int> getOrder(vector<vector<int>> &tasks) {4
// we use priority queue to get the least processing time from the available5
// server implement a min heap9
using dp = pair<long int, pair<long int, long int>>;10
using sp = pair<long int, long int>;11
priority_queue<sp, vector<sp>, greater<sp>> pq;12
int len = tasks.size();13
// we can rearrage the tasks but we can't get the index in the original16
for (long int i = 0; i < len; i++) {17
rearrange.push_back({tasks[i][0], {tasks[i][1], i}});20
// rearrange contains the same as tasks but with extra value "index" in its23
// sort in the ascending order of their enqueue time24
// if two tasks have same enqueue time it will sort the one which has the25
// less processing time26
sort(rearrange.begin(), rearrange.end());28
long int finishTime = rearrange[0].first;29
long int k = tasks.size();33
while (i < len && finishTime >= rearrange[i].first) {34
// push the processing time and the index35
pq.push({rearrange[i].second.first, rearrange[i].second.second});39
// pick the task which is available upto the current finishTime and with40
// the less processing time41
auto [time, ind] = pq.top();44
// processing the tasks take "time"45
finishTime += time; // the cpu is now idle at the time finishTime48
// now i points to the next task49
// if there are no tasks left in pq50
// and the next tasks enqueue time is larger than the current finishing52
// we start with the task enqueue time53
if (pq.empty() && (i < len && finishTime < rearrange[i].first))54
finishTime = rearrange[i].first;