sweet1cris

Untitled

Feb 9th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     /*
  4.      * @param : duration and close day of each course
  5.      * @return: the maximal number of courses that can be taken
  6.      */
  7.     public int scheduleCourse(int[][] courses) {
  8.         // write your code here
  9.         if (courses == null || courses.length == 0) {
  10.             return 0;
  11.         }
  12.         Arrays.sort(courses, (a, b) -> a[1] - b[1]);
  13.         PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
  14.         int time = 0;
  15.         for (int i = 0; i < courses.length; i++) {
  16.             if (time + courses[i][0] <= courses[i][1]) {
  17.                 time += courses[i][0];
  18.                 queue.offer(courses[i][0]);
  19.             }
  20.             else if (!queue.isEmpty() && courses[i][0] < queue.peek()) {
  21.                 time += courses[i][0] - queue.poll();
  22.                 queue.offer(courses[i][0]);
  23.             }
  24.         }
  25.         return queue.size();
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment