Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /*
- * @param : duration and close day of each course
- * @return: the maximal number of courses that can be taken
- */
- public int scheduleCourse(int[][] courses) {
- // write your code here
- if (courses == null || courses.length == 0) {
- return 0;
- }
- Arrays.sort(courses, (a, b) -> a[1] - b[1]);
- PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
- int time = 0;
- for (int i = 0; i < courses.length; i++) {
- if (time + courses[i][0] <= courses[i][1]) {
- time += courses[i][0];
- queue.offer(courses[i][0]);
- }
- else if (!queue.isEmpty() && courses[i][0] < queue.peek()) {
- time += courses[i][0] - queue.poll();
- queue.offer(courses[i][0]);
- }
- }
- return queue.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment