-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-ThreadedCPU.cpp
More file actions
31 lines (30 loc) · 894 Bytes
/
single-ThreadedCPU.cpp
File metadata and controls
31 lines (30 loc) · 894 Bytes
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
26
27
28
29
30
31
// Source: https://leetcode.com/problems/single-threaded-cpu/
// Author: Miao Zhang
// Date: 2021-06-10
class Solution {
public:
vector<int> getOrder(vector<vector<int>>& tasks) {
int n = tasks.size();
for (int i = 0; i < n; i++) {
tasks[i].push_back(i);
}
sort(begin(tasks), end(tasks));
vector<int> res;
priority_queue<pair<int, int>> q; // {-processtime, -index}
int i = 0;
long cur = 0;
while (res.size() != n) {
if (i < n && q.empty() && tasks[i][0] > cur) {
cur = tasks[i][0];
}
while (i < n && tasks[i][0] <= cur) {
q.emplace(-tasks[i][1], -tasks[i][2]);
i++;
}
cur -= q.top().first;
res.push_back(-q.top().second);
q.pop();
}
return res;
}
};