|
| 1 | +--- |
| 2 | +id: course-schedule-III |
| 3 | +title: Course Schedule III |
| 4 | +sidebar_label: 0630 - Course Schedule III |
| 5 | +tags: |
| 6 | + - Heap |
| 7 | + - Array |
| 8 | + - Greedy |
| 9 | + - Sorting |
| 10 | +description: "This is a solution to the Course Schedule III problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +There are n different online courses numbered from `1 to n`. You are given an array courses where `courses[i] = [durationi, lastDayi]` indicate that the `ith` course should be taken continuously for durationi days and must be finished before or on lastDayi. |
| 16 | + |
| 17 | +You will start on the 1st day and you cannot take two or more courses simultaneously. |
| 18 | + |
| 19 | +Return the maximum number of courses that you can take. |
| 20 | + |
| 21 | +### Examples |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | +``` |
| 26 | +Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]] |
| 27 | +Output: 3 |
| 28 | +Explanation: |
| 29 | +There are totally 4 courses, but you can take 3 courses at most: |
| 30 | +First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day. |
| 31 | +Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. |
| 32 | +Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. |
| 33 | +The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date |
| 34 | +
|
| 35 | +``` |
| 36 | +**Example 2:** |
| 37 | +``` |
| 38 | +Input: courses = [[1,2]] |
| 39 | +Output: 1 |
| 40 | +
|
| 41 | +``` |
| 42 | +### Constraints |
| 43 | + |
| 44 | +- `1 <= courses.length <= 10^4` |
| 45 | +- `1 <= durationi, lastDayi <= 10^4` |
| 46 | + |
| 47 | +## Solution for Course Schedule III |
| 48 | + |
| 49 | +### Approach |
| 50 | + |
| 51 | +Sort all the courses by their ending time. When considering the first `K` courses, they all end before end. A necessary and sufficient condition for our schedule to be valid, is that `(for all K)`, the courses we choose to take within the first K of them, have total duration less than end. |
| 52 | + |
| 53 | +For each `K`, we will greedily remove the largest-length course until the total duration `start is <= end`. To select these largest-length courses, we will use a max heap. start will maintain the loop invariant that it is the sum of the lengths of the courses we have currently taken. |
| 54 | + |
| 55 | +Clearly, this greedy choice makes the number of courses used maximal for each K. When considering potential future K, there's never a case where we preferred having a longer course to a shorter one, so indeed our greedy choice dominates all other candidates. |
| 56 | + |
| 57 | + |
| 58 | +## Code in Different Languages |
| 59 | + |
| 60 | +<Tabs> |
| 61 | +<TabItem value="cpp" label="C++"> |
| 62 | + <SolutionAuthor name="@agarwalhimanshugaya"/> |
| 63 | + |
| 64 | +```cpp |
| 65 | +class Solution { |
| 66 | +public: |
| 67 | + int scheduleCourse(vector<vector<int>>& courses) { |
| 68 | + if(courses.size() <= 0) return 0; |
| 69 | + sort(courses.begin(), courses.end(), [](const vector<int>& a, vector<int>& b) { |
| 70 | + return a[1] < b[1]; |
| 71 | + }); |
| 72 | + priority_queue<int> q; |
| 73 | + int sum = 0; |
| 74 | + for(auto i : courses) { |
| 75 | + sum += i[0]; |
| 76 | + q.push(i[0]); |
| 77 | + if(sum > i[1]) { |
| 78 | + sum -= q.top(); |
| 79 | + q.pop(); |
| 80 | + } |
| 81 | + } |
| 82 | + return q.size(); |
| 83 | + } |
| 84 | +}; |
| 85 | +``` |
| 86 | +</TabItem> |
| 87 | +<TabItem value="java" label="Java"> |
| 88 | + <SolutionAuthor name="@agarwalhimanshugaya"/> |
| 89 | +
|
| 90 | +```java |
| 91 | +public class Solution { |
| 92 | + public int scheduleCourse(int[][] courses) { |
| 93 | + 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) |
| 94 | + PriorityQueue<Integer> pq=new PriorityQueue<>((a,b)->b-a); |
| 95 | + int time=0; |
| 96 | + for (int[] c:courses) |
| 97 | + { |
| 98 | + time+=c[0]; // add current course to a priority queue |
| 99 | + pq.add(c[0]); |
| 100 | + 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!) |
| 101 | + } |
| 102 | + return pq.size(); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +</TabItem> |
| 108 | +<TabItem value="python" label="Python"> |
| 109 | + <SolutionAuthor name="@agarwalhimanshugaya"/> |
| 110 | + |
| 111 | +```python |
| 112 | +def scheduleCourse(self, A): |
| 113 | + pq = [] |
| 114 | + start = 0 |
| 115 | + for t, end in sorted(A, key = lambda (t, end): end): |
| 116 | + start += t |
| 117 | + heapq.heappush(pq, -t) |
| 118 | + while start > end: |
| 119 | + start += heapq.heappop(pq) |
| 120 | + return len(pq) |
| 121 | +``` |
| 122 | +</TabItem> |
| 123 | +</Tabs> |
| 124 | + |
| 125 | +## Complexity Analysis |
| 126 | + |
| 127 | +### Time Complexity: $O(N⋅log(N))$ |
| 128 | + |
| 129 | +### Space Complexity: $O(N)$ |
| 130 | + |
| 131 | +## References |
| 132 | + |
| 133 | +- **LeetCode Problem**: [Kth Largest Element in a Stream](https://leetcode.com/problems/course-schedule-iii/description/) |
| 134 | + |
| 135 | +- **Solution Link**: [Kth Largest Element in a Stream](https://leetcode.com/problems/course-schedule-iii/solutions/) |
0 commit comments