-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPUScheduling.cpp
55 lines (50 loc) · 1.28 KB
/
CPUScheduling.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// https://binarysearch.com/problems/CPU-Scheduling
class Compare
{
public:
bool operator () (vector<int>& v1, vector<int>& v2)
{
if (v1[2] == v2[2])
return v1[0] > v2[0];
return v1[2] > v2[2];
}
};
bool cmp(vector<int>& v1, vector<int>& v2)
{
if (v1[1] == v2[1])
{
if (v1[2] == v2[2])
return v1[0] < v2[0];
return v1[2] < v2[2];
}
return v1[1] < v2[1];
}
vector<int> solve(vector<vector<int>>& tasks) {
int n = (int)tasks.size();
sort(begin(tasks), end(tasks), cmp);
priority_queue<vector<int>, vector<vector<int>>, Compare> min_heap;
vector<int> ans;
int idx = 0;
int queue_time = tasks[0][1];
min_heap.push(tasks[idx]);
idx++;
while (!min_heap.empty())
{
vector<int> cur = min_heap.top();
ans.push_back(cur[0]);
min_heap.pop();
queue_time += cur[2];
while (idx < n && tasks[idx][1] <= queue_time)
{
min_heap.push(tasks[idx]);
idx++;
}
if (idx < n && min_heap.empty())
{
queue_time = tasks[idx][1];
min_heap.push(tasks[idx]);
idx++;
}
}
return ans;
}