2차원 정수 벡터를 받는데 두번째 축의 첫번째 아이템은 queue에 task가 들어오는 시간이
적혀있고, 두번째 아이템은 task를 처리하는 시간이 적혀있다.
문제는 cpu가 특정 인덱스의 task를 몇번째 순서로 처리하는지 구하여 반환해야 한다.
cpu는 한 번에 하나의 task만 처리할 수 있고 아래의 조건에 따라 task를 처리한다
If the CPU is idle and there are no available tasks to process, the CPU remains idle. If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index. Once a task is started, the CPU will process the entire task without stopping. The CPU can finish a task then start a new one instantly.
class Solution {
public:
vector<int> getOrder(vector<vector<int>>& tasks) {
int length = tasks.size();
vector<int> processingOrder{};
for (int i = 0; i < length; i++)
{
tasks[i].push_back(i);
}
sort(tasks.begin(), tasks.end(), [](auto &task1, auto &task2)
{
return task1[0] < task2[0];
});
auto customCompare = [](const pair<int, int> &task1, const pair<int, int> &task2)
{
if (task1.second == task2.second)
{
return task1.first > task2.first;
}
return task1.second > task2.second;
};
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(customCompare)> pq(customCompare);
int taskIndex{0};
long long int time{0};
while (taskIndex < length || pq.size())
{
while (taskIndex < length && tasks[taskIndex][0] <= time)
{
pq.push({tasks[taskIndex][2], tasks[taskIndex][1]});
taskIndex++;
}
if (pq.empty())
{
time = tasks[taskIndex][0];
continue;
}
processingOrder.push_back(pq.top().first);
time += pq.top().second;
pq.pop();
}
return processingOrder;
}
};