Posted on

Description

Submission

class Solution {
    typedef pair<int, int> PII; 
public:
    vector<int> getOrder(vector<vector<int>>& tasks) {
        for(int i = 0; i < tasks.size(); ++i) {
            tasks[i].push_back(i);
        }
        sort(tasks.begin(), tasks.end());
        
        vector<int> rets;

        priority_queue<PII, vector<PII>, greater<>> pq;      // processing time, index
        long long t = tasks[0][0], i = 0;

        for(; i < tasks.size(); ++i) {
            if(t == tasks[i][0]) {
                pq.emplace(tasks[i][1], tasks[i][2]);      
            } else {
                break;
            }
        }


        while(!pq.empty()) {
            auto [processingTime, index] = pq.top();
            // cout << index << endl;
            pq.pop();
            rets.push_back(index);
            
            t += processingTime;
            if(pq.empty() && i < tasks.size() && tasks[i][0] > t) t = tasks[i][0];
            for(; i < tasks.size(); ++i) {
                if(tasks[i][0] <= t) {
                    pq.emplace(tasks[i][1], tasks[i][2]);
                } else {
                    break;
                }
            }
        }

        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *