forked from black-shadows/LeetCode-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-threaded-cpu.cpp
25 lines (24 loc) · 907 Bytes
/
single-threaded-cpu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
vector<int> getOrder(vector<vector<int>>& tasks) {
vector<int> result, idx(tasks.size());
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> min_heap;
iota(begin(idx), end(idx), 0);
sort(begin(idx), end(idx), [&](int i, int j) { return tasks[i][0] < tasks[j][0]; });
for (int64_t i = 0, time = tasks[idx[i]][0]; i < size(idx) || !empty(min_heap);) {
for (; i < size(idx) && tasks[idx[i]][0] <= time; ++i) {
min_heap.emplace(tasks[idx[i]][1], idx[i]);
}
if (empty(min_heap)) {
time = tasks[idx[i]][0];
continue;
}
const auto [t, j] = min_heap.top(); min_heap.pop();
time += t;
result.emplace_back(j);
}
return result;
}
};