630. Course Schedule III LeetCode Solution

In this guide, you will get 630. Course Schedule III LeetCode Solution with the best time and space complexity. The solution to Course Schedule III problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Course Schedule III solution in C++
  4. Course Schedule III solution in Java
  5. Course Schedule III solution in Python
  6. Additional Resources
630. Course Schedule III LeetCode Solution image

Problem Statement of Course Schedule III

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.
You will start on the 1st day and you cannot take two or more courses simultaneously.
Return the maximum number of courses that you can take.

Example 1:

Input: courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
Output: 3
Explanation:
There are totally 4 courses, but you can take 3 courses at most:
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.
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.
Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day.
The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.

Example 2:

Input: courses = [[1,2]]
Output: 1

Example 3:

Input: courses = [[3,2],[4,3]]
Output: 0

Constraints:

1 <= courses.length <= 104
1 <= durationi, lastDayi <= 104

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

630. Course Schedule III LeetCode Solution in C++

class Solution {
 public:
  int scheduleCourse(vector<vector<int>>& courses) {
    int time = 0;
    priority_queue<int> maxHeap;

    ranges::sort(courses, ranges::less{},
                 [](const vector<int>& course) { return course[1]; });

    for (const vector<int>& course : courses) {
      const int duration = course[0];
      const int lastDay = course[1];
      maxHeap.push(duration);
      time += duration;
      // If the current course cannot be taken, check if it can be swapped with
      // a previously taken course that has a larger duration to increase the
      // time available to take upcoming courses.
      if (time > lastDay)
        time -= maxHeap.top(), maxHeap.pop();
    }

    return maxHeap.size();
  }
};
/* code provided by PROGIEZ */

630. Course Schedule III LeetCode Solution in Java

class Solution {
  public int scheduleCourse(int[][] courses) {
    int time = 0;
    Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

    Arrays.sort(courses, (a, b) -> Integer.compare(a[1], b[1]));

    for (int[] course : courses) {
      final int duration = course[0];
      final int lastDay = course[1];
      maxHeap.offer(duration);
      time += course;
      // If the current course cannot be taken, check if it can be swapped with
      // a previously taken course that has a larger duration to increase the
      // time available to take upcoming courses.
      if (time > lastDay)
        time -= maxHeap.poll();
    }

    return maxHeap.size();
  }
}
// code provided by PROGIEZ

630. Course Schedule III LeetCode Solution in Python

class Solution:
  def scheduleCourse(self, courses: list[list[int]]) -> int:
    time = 0
    maxHeap = []

    for duration, lastDay in sorted(courses, key=lambda x: x[1]):
      heapq.heappush(maxHeap, -duration)
      time += duration
      # If the current course cannot be taken, check if it can be swapped with
      # a previously taken course that has a larger duration to increase the
      # time available to take upcoming courses.
      if time > lastDay:
        time += heapq.heappop(maxHeap)

    return len(maxHeap)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.