Course Schedule - Complete Solution Guide
Course Schedule is LeetCode problem 207, a Medium 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 a total of numCourses courses you have to take, labeled from 0 to numCourses - 1 . You are given an array prerequisites where prerequisites[i] = [a i , b i ] indicates that you must take course b i first if you want to take course a i . For example, the pair [0, 1] , indicates that to take course 0 you have to first take course 1 . Return true if you can finish all courses. Otherwise, return false . Example 1: Input: numCourses = 2, prerequisites = [[1,0]] Output: true Explanation: The
Detailed Explanation
The problem 'Course Schedule' asks us to determine if it's possible to finish all courses given a number of courses and a set of prerequisites. Each prerequisite is a pair [a, b], meaning that you must take course 'b' before you can take course 'a'. We need to determine if there's a cycle in the dependency graph of courses, because a cycle would make it impossible to complete all courses. If a cycle exists, return false, otherwise return true.
Solution Approach
The provided code uses a topological sort algorithm based on Kahn's algorithm using in-degree. It constructs an adjacency list representation of the course dependencies. It then calculates the in-degree (number of prerequisites) for each course. Courses with an in-degree of 0 are added to a queue. The algorithm then iteratively removes a course from the queue, decrements the in-degree of its dependent courses, and adds dependent courses with an in-degree of 0 to the queue. Finally, it checks if the number of courses finished equals the total number of courses. If they are equal, then all courses can be taken.
Step-by-Step Algorithm
- Step 1: Create an adjacency list to represent the course dependencies. `adj[i]` stores a list of courses that depend on course `i`.
- Step 2: Calculate the in-degree of each course. The in-degree of a course is the number of prerequisite courses it has.
- Step 3: Initialize a queue with courses that have an in-degree of 0 (no prerequisites).
- Step 4: While the queue is not empty:
- a. Remove a course from the front of the queue.
- b. Increment the `courses_finished` counter.
- c. For each neighbor (dependent course) of the removed course:
- i. Decrement the in-degree of the neighbor.
- ii. If the in-degree of the neighbor becomes 0, add it to the queue.
- Step 5: Return `true` if `courses_finished` is equal to the total number of courses, indicating that all courses can be taken. Otherwise, return `false`.
Key Insights
- Insight 1: The problem can be modeled as a directed graph where courses are nodes and prerequisites are directed edges.
- Insight 2: Detecting cycles in a directed graph is crucial. Topological sort can be used to detect cycles.
- Insight 3: If a topological sort can be completed successfully (i.e., all nodes are visited), there are no cycles, and all courses can be finished. Otherwise, a cycle exists.
Complexity Analysis
Time Complexity: O(V+E)
Space Complexity: O(V+E)
Topics
This problem involves: Depth-First Search, Breadth-First Search, Graph, Topological Sort.
Companies
Asked at: Anduril, ByteDance, Citadel, Coupang, CrowdStrike, Cruise, DoorDash, Flipkart, Graviton, IXL, Intuit, Karat, LiveRamp, Nordstrom, Nutanix, Nvidia, Oracle, PayPal, Roblox, Snap, Snowflake, Swiggy, VMware, Visa, Walmart Labs, Yelp, Zenefits, eBay.