Advertisement

Course Schedule III - LeetCode 630 Solution

Course Schedule III - Complete Solution Guide

Course Schedule III is LeetCode problem 630, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There are n different online courses numbered from 1 to n . You are given an array courses where courses[i] = [duration i , lastDay i ] indicate that the i th course should be taken continuously for duration i days and must be finished before or on lastDay i . You will start on the 1 st 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 Explanatio

Detailed Explanation

The problem asks us to find the maximum number of courses we can take, given a list of courses. Each course has a duration and a last day it can be completed. We must take each course contiguously, and we cannot take two courses simultaneously. We must start on day 1 and determine the largest number of courses that can be completed within their deadlines.

Solution Approach

The solution uses a greedy approach combined with a max-heap. First, the courses are sorted based on their 'lastDay'. Then, the algorithm iterates through the sorted courses, keeping track of the 'current_time' used and maintaining a max-heap of the course durations that have been added so far. For each course, the duration is added to the current time, and the duration is added to the heap. If the current time exceeds the course's 'lastDay', the largest duration in the heap is removed (which corresponds to the longest course currently scheduled), and the current time is adjusted accordingly. At the end, the number of courses in the heap represents the maximum number of courses that can be taken.

Step-by-Step Algorithm

  1. Step 1: Sort the courses based on their last day (deadline) in ascending order. This prioritizes courses that need to be completed sooner.
  2. Step 2: Initialize a max-heap to store the durations of the courses taken so far. This allows efficient retrieval and removal of the course with the longest duration.
  3. Step 3: Initialize a variable 'current_time' to 0, representing the current time spent taking courses.
  4. Step 4: Iterate through the sorted courses. For each course:
  5. Step 5: Add the course's duration to 'current_time'.
  6. Step 6: Push the course's duration onto the max-heap.
  7. Step 7: If 'current_time' is greater than the course's 'lastDay', it means we've exceeded the deadline. In this case, remove the largest duration from the max-heap and subtract it from 'current_time'. This simulates removing the longest course we've already taken.
  8. Step 8: After iterating through all courses, the size of the max-heap represents the maximum number of courses that can be taken.

Key Insights

  • Insight 1: Sorting courses by their last day allows us to prioritize courses with earlier deadlines, making it more likely to fit more courses into our schedule.
  • Insight 2: Using a max-heap (priority queue) helps us keep track of the durations of the courses we've currently scheduled. This allows us to efficiently remove the longest course if adding a new course exceeds its deadline.
  • Insight 3: The greedy approach of sorting by last day and using a max-heap works because if adding a new course makes us exceed its deadline, we can always remove the longest currently scheduled course to potentially free up enough time to accommodate the new course while still keeping the number of courses taken as high as possible.

Complexity Analysis

Time Complexity: O(nlogn)

Space Complexity: O(n)

Topics

This problem involves: Array, Greedy, Sorting, Heap (Priority Queue).

Companies

Asked at: Flipkart, Works Applications.