-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1834.cpp
More file actions
40 lines (38 loc) · 1.3 KB
/
1834.cpp
File metadata and controls
40 lines (38 loc) · 1.3 KB
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
32
33
34
35
36
37
38
39
40
typedef pair<int ,int> P;
class Solution {
public:
vector<int> getOrder(vector<vector<int>>& tasks) {
priority_queue<P, vector<P>, greater<P>> pq;
int n = tasks.size();
vector<int> res;
vector<vector<int>> taskWithId(n, {0, 0, 0});
for (int i = 0; i < n; ++i) {
taskWithId[i][0] = tasks[i][0];
taskWithId[i][1] = tasks[i][1];
taskWithId[i][2] = i;
}
sort(taskWithId.begin(), taskWithId.end());
int index = 0;
long long currentTime = taskWithId[index][0];
while (index < n && taskWithId[index][0] <= currentTime) {
pq.push({taskWithId[index][1], taskWithId[index][2]});
index++;
}
while (!pq.empty()) {
auto [duration, taskId] = pq.top();
res.push_back(taskId);
pq.pop();
currentTime += duration;
while (index < n && taskWithId[index][0] <= currentTime) {
pq.push({taskWithId[index][1], taskWithId[index][2]});
index++;
}
if (pq.empty() && index < n) {
currentTime = taskWithId[index][0];
pq.push({taskWithId[index][1], taskWithId[index][2]});
index++;
}
}
return res;
}
};