Course Schedule II - Complete Solution Guide
Course Schedule II is LeetCode problem 210, 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 the ordering of courses you should take to finish all courses . If there are many valid answers, return any of them. If it is impossible to finish al
Detailed Explanation
The problem asks us to find a valid order in which to take a set of courses, given dependencies between them. These dependencies are represented as pairs [course, prerequisite], meaning you must take 'prerequisite' before 'course'. The goal is to return a list of courses that can be taken in order to complete all courses. If it's impossible to finish all courses due to a cyclic dependency, return an empty array. 'numCourses' represents the total number of courses, labeled from 0 to numCourses - 1. 'prerequisites' is a list of lists where each inner list has two integers [course, prerequisite]. The output should be a list representing a valid order to take courses, or an empty list if no such order exists.
Solution Approach
The provided solution uses Kahn's algorithm (a Breadth-First Search (BFS) approach) to perform topological sorting. It calculates the in-degree of each course (number of prerequisites) and initializes a queue with all courses that have an in-degree of 0 (no prerequisites). It then iteratively removes courses from the queue, adds them to the topological order, and reduces the in-degree of their neighbors. If a neighbor's in-degree becomes 0, it's added to the queue. If the number of courses in the topological order equals the total number of courses, a valid order has been found. Otherwise, a cycle exists, and an empty array is returned.
Step-by-Step Algorithm
- Step 1: Build the adjacency list representation of the graph from the 'prerequisites' list. Also, compute the in-degree of each course.
- Step 2: Initialize a queue with all courses that have an in-degree of 0.
- Step 3: While the queue is not empty, dequeue a course, add it to the 'topological_order' list.
- Step 4: For each neighbor of the dequeued course, decrement its in-degree. If the neighbor's in-degree becomes 0, enqueue it.
- Step 5: After the queue is empty, check if the size of the 'topological_order' list is equal to 'numCourses'. If it is, return the 'topological_order' list. Otherwise, return an empty list, indicating the presence of a cycle.
Key Insights
- Insight 1: The problem can be modeled as a directed graph, where courses are nodes and prerequisites are directed edges. A valid course schedule corresponds to a topological sort of this graph.
- Insight 2: The presence of cycles in the graph indicates that it is impossible to complete all courses (a cyclic dependency).
- Insight 3: We can use Kahn's algorithm (using in-degrees and a queue) or Depth-First Search (DFS) to detect cycles and perform topological sorting.
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, Arista Networks, ByteDance, Citadel, Databricks, DoorDash, Intuit, Karat, Netflix, Nvidia, Oracle, PhonePe, Qualcomm, Remitly, Salesforce, Snap, Snowflake, Tesla, VMware, Walmart Labs, Workday, Zenefits, eBay.