Advertisement
lifeiteng

630. Course Schedule III

Sep 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. class Solution {
  2.     public int scheduleCourse(int[][] courses) {
  3.         Arrays.sort(courses, (a, b) ->
  4.             (a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]));
  5.         int end = 0, re = 0, last = 0;
  6.         Queue<Integer> q = new PriorityQueue<>((a, b)->(b - a));
  7.         for(int[] c : courses)
  8.         {
  9.             if(end + c[0] <= c[1])
  10.             {
  11.                 re++;
  12.                 end += c[0];
  13.                 q.offer(c[0]);
  14.             }
  15.             else if(!q.isEmpty() && q.peek() > c[0])
  16.             {
  17.                 // re++;
  18.                 end += c[0] - q.poll();
  19.                 q.offer(c[0]);
  20.             }
  21.         }
  22.         return re;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement