classSolution{public:intscheduleCourse(vector<vector<int>>&courses){inttime=0;sort(begin(courses),end(courses),[](constauto&a,constauto&b){returna[1]<b[1];});priority_queue<int>maxHeap;for(constvector<int>&c:courses){constintduration=c[0];constintlastDay=c[1];maxHeap.push(duration);time+=c[0];// If current course could not be taken, check if it's able to swap with a// Previously taken course with larger duration, to increase the time// Available to take upcoming coursesif(time>lastDay)time-=maxHeap.top(),maxHeap.pop();}returnmaxHeap.size();}};
classSolution{publicintscheduleCourse(int[][]courses){inttime=0;Arrays.sort(courses,(a,b)->(a[1]-b[1]));Queue<Integer>maxHeap=newPriorityQueue<>((a,b)->b-a);for(int[]c:courses){finalintduration=c[0];finalintlastDay=c[1];maxHeap.offer(duration);time+=c[0];// If current course could not be taken, check if it's able to swap with a// Previously taken course with larger duration, to increase the time// Available to take upcoming coursesif(time>lastDay)time-=maxHeap.poll();}returnmaxHeap.size();}}
classSolution:defscheduleCourse(self,courses:List[List[int]])->int:time=0maxHeap=[]forduration,lastDayinsorted(courses,key=lambdax:x[1]):heapq.heappush(maxHeap,-duration)time+=duration# If current course could not be taken, check if it's able to swap with a# Previously taken course with larger duration, to increase the time# Available to take upcoming coursesiftime>lastDay:time+=heapq.heappop(maxHeap)returnlen(maxHeap)