Short Java code using PriorityQueue


  • 32
    K
    public class Solution {
        public int scheduleCourse(int[][] courses) {
            Arrays.sort(courses,(a,b)->a[1]-b[1]); //Sort the courses by their deadlines (Greedy! We have to deal with courses with early deadlines first)
            PriorityQueue<Integer> pq=new PriorityQueue<>((a,b)->b-a);
            int time=0;
            for (int[] c:courses) 
            {
                time+=c[0]; // add current course to a priority queue
                pq.add(c[0]);
                if (time>c[1]) time-=pq.poll(); //If time exceeds, drop the previous course which costs the most time. (That must be the best choice!)
            }        
            return pq.size();
        }
    }
    

  • 0
    T

    This is a great solution! Thank you!


  • 0
    F

    Great job,thx


  • 0
    I

    Genius!and very clear explanation.


  • 0
    L

    Smart solution! Thank you!


Log in to reply
 

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