参考:https://leetcode-cn.com/problems/single-threaded-cpu/submissions/
class Solution {
public:
struct data{
int start,duration,id;
bool operator <(const data &b)const{
if(duration>b.duration)return 1;
if(duration<b.duration)return 0;
return id>b.id;
}
};
static bool cmptask(vector<int> &a,vector<int> &b){//注意在class中的cmp函数需要加入static,对vector<int>向量组为关键字排序需要加入&
return a[0]<b[0];
}
vector<int> getOrder(vector<vector<int>>& tasks) {
int n=tasks.size();
for(int i=0;i<n;i++)tasks[i].push_back(i);
sort(tasks.begin(),tasks.end(),cmptask);
priority_queue<data>q;
long long time=0;
int flag=0;
vector<int>res;
while(1){
while(flag<n&&time>=tasks[flag][0]){
q.push({tasks[flag][0],tasks[flag][1],tasks[flag][2]});
flag++;
}
if(q.size()>0){
res.push_back(q.top().id);
time+=q.top().duration;
q.pop();
}else{
if(flag==n)break;
time=tasks[flag][0];
}
}
return res;
}
};
注意在class中的cmp函数需要加入static,对vector向量组为关键字排序向量变量前需要加入&