|
3 | 3 | // #Hard #Array #Greedy #Heap_Priority_Queue
|
4 | 4 |
|
5 | 5 | import java.util.Arrays;
|
| 6 | +import java.util.PriorityQueue; |
6 | 7 |
|
7 | 8 | @SuppressWarnings("java:S135")
|
8 | 9 | public class Solution {
|
9 | 10 | public int scheduleCourse(int[][] courses) {
|
| 11 | + // Sort the courses based on their deadline date. |
10 | 12 | Arrays.sort(courses, (a, b) -> a[1] - b[1]);
|
11 | | - int course = 0; |
| 13 | + // Only the duration is stored. We don't care which course |
| 14 | + // is the longest, we only care about the total courses can |
| 15 | + // be taken. |
| 16 | + // If the question wants the course ids to be returned. |
| 17 | + // Consider use a Pair<Duration, CourseId> int pair. |
| 18 | + PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a); |
| 19 | + // Total time consumed. |
12 | 20 | int time = 0;
|
13 | | - MaxHeap heap = new MaxHeap(courses.length); |
14 | | - for (int[] cours : courses) { |
15 | | - if (cours[1] - time >= cours[0]) { |
16 | | - time += cours[0]; |
17 | | - course++; |
18 | | - heap.add(cours[0]); |
19 | | - } else if (cours[0] < heap.getHeap()[0]) { |
20 | | - int t = heap.pop(); |
21 | | - heap.add(cours[0]); |
22 | | - time = time - t + cours[0]; |
23 | | - } |
24 | | - } |
25 | | - return course; |
26 | | - } |
27 | | - |
28 | | - static class MaxHeap { |
29 | | - private final int[] heap; |
30 | | - private int pin; |
31 | | - |
32 | | - public MaxHeap(int mexLen) { |
33 | | - this.heap = new int[mexLen]; |
34 | | - this.pin = 0; |
35 | | - } |
36 | | - |
37 | | - public int[] getHeap() { |
38 | | - return heap; |
39 | | - } |
40 | | - |
41 | | - public void add(int e) { |
42 | | - heap[pin] = e; |
43 | | - int temp = pin; |
44 | | - pin++; |
45 | | - while (temp > 0 && heap[(temp - 1) / 2] < e) { |
46 | | - heap[temp] = heap[(temp - 1) / 2]; |
47 | | - temp = (temp - 1) / 2; |
48 | | - heap[temp] = e; |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - public int pop() { |
53 | | - int res = heap[0]; |
54 | | - pin--; |
55 | | - heap[0] = heap[pin]; |
56 | | - int h0 = heap[0]; |
57 | | - int temp = 0; |
58 | | - while (temp * 2 + 1 < pin) { |
59 | | - if (temp * 2 + 2 == pin) { |
60 | | - if (heap[temp * 2 + 1] > h0) { |
61 | | - heap[temp] = heap[temp * 2 + 1]; |
62 | | - temp = temp * 2 + 1; |
63 | | - heap[temp] = h0; |
64 | | - } else { |
65 | | - break; |
66 | | - } |
67 | | - } else { |
68 | | - if (h0 < heap[temp * 2 + 1] || h0 < heap[temp * 2 + 2]) { |
69 | | - if (heap[temp * 2 + 1] > heap[temp * 2 + 2]) { |
70 | | - heap[temp] = heap[temp * 2 + 1]; |
71 | | - temp = temp * 2 + 1; |
72 | | - } else { |
73 | | - heap[temp] = heap[temp * 2 + 2]; |
74 | | - temp = temp * 2 + 2; |
75 | | - } |
76 | | - heap[temp] = h0; |
77 | | - } else { |
78 | | - break; |
79 | | - } |
| 21 | + // At the given time `course`, the overall "time limit" is |
| 22 | + // course[1]. All courses in pq is already 'valid'. But |
| 23 | + // adding this course[0] might exceed the course[1] limit. |
| 24 | + for (int[] course : courses) { |
| 25 | + // If adding this course doesn't exceed. Let's add it |
| 26 | + // for now. (Greedy algo). We might remove it later if |
| 27 | + // we have a "better" solution at that time. |
| 28 | + if (time + course[0] <= course[1]) { |
| 29 | + time += course[0]; |
| 30 | + pq.offer(course[0]); |
| 31 | + } else { |
| 32 | + // If adding this ecxeeds the limit. We can still add it |
| 33 | + // if-and-only-if there are courses longer than current |
| 34 | + // one. If so, by removing a longer course, current shorter |
| 35 | + // course can fit in for sure. Although the total course |
| 36 | + // count is the same, the overall time consumed is shorter. |
| 37 | + // Which gives us more room for future courses. |
| 38 | + // Remove any course that is longer than current course |
| 39 | + // will work, but we remove the longest one with the help |
| 40 | + // of heap (pq). |
| 41 | + if (!pq.isEmpty() && pq.peek() > course[0]) { |
| 42 | + time -= pq.poll(); |
| 43 | + time += course[0]; |
| 44 | + pq.offer(course[0]); |
80 | 45 | }
|
| 46 | + // If no course in consider (pq) is shorter than the |
| 47 | + // current course. It is safe to discard it. |
81 | 48 | }
|
82 | | - return res; |
83 | 49 | }
|
| 50 | + return pq.size(); |
84 | 51 | }
|
85 | 52 | }
|
0 commit comments