C++ priority queue solution


  • 0
    J
    class Solution {
    public:
        int scheduleCourse(vector<vector<int>>& courses) {
            sort(courses.begin(),courses.end(),[](vector<int>a,vector<int>b){return a.back()<b.back();});
            priority_queue<int> pq;
            int time = 0;
            for(auto course:courses)
            {
                pq.push(course[0]);
                time+=course[0];
                if(time>course[1]){time-=pq.top(),pq.pop();}
            }
            return pq.size();  
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.