Solving Leetcode Interviews in Seconds with AI: Course Schedule IV
Introduction
In this blog post, we will explore how to solve the LeetCode problem "1462" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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] = [ai, bi] indicates that you must take course ai first if you want to take course bi. For example, the pair [0, 1] indicates that you have to take course 0 before you can take course 1. Prerequisites can also be indirect. If course a is a prerequisite of course b, and course b is a prerequisite of course c, then course a is a prerequisite of course c. You are also given an array queries where queries[j] = [uj, vj]. For the jth query, you should answer whether course uj is a prerequisite of course vj or not. Return a boolean array answer, where answer[j] is the answer to the jth query. Example 1: Input: numCourses = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]] Output: [false,true] Explanation: The pair [1, 0] indicates that you have to take course 1 before you can take course 0. Course 0 is not a prerequisite of course 1, but the opposite is true. Example 2: Input: numCourses = 2, prerequisites = [], queries = [[1,0],[0,1]] Output: [false,false] Explanation: There are no prerequisites, and each course is independent. Example 3: Input: numCourses = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]] Output: [true,true] Constraints: 2 <= numCourses <= 100 0 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2) prerequisites[i].length == 2 0 <= ai, bi <= numCourses - 1 ai != bi All the pairs [ai, bi] are unique. The prerequisites graph has no cycles. 1 <= queries.length <= 104 0 <= ui, vi <= numCourses - 1 ui != vi
Explanation
- Build a graph and compute transitive closure: Represent the prerequisites as a directed graph. Use the Floyd-Warshall algorithm to compute the transitive closure of the graph, which indicates whether there is a path between any two courses.
- Answer queries: For each query (u, v), check if there is a path from u to v in the transitive closure.
- Optimization: Floyd-Warshall's algorithm efficiently computes all-pairs reachability.
- Runtime Complexity: O(N^3 + Q), where N is the number of courses and Q is the number of queries.
- Storage Complexity: O(N^2)
Code
def solve():
numCourses = int(input())
prerequisites = eval(input())
queries = eval(input())
adj_matrix = [[False] * numCourses for _ in range(numCourses)]
for u, v in prerequisites:
adj_matrix[u][v] = True
# Floyd-Warshall Algorithm for Transitive Closure
for k in range(numCourses):
for i in range(numCourses):
for j in range(numCourses):
adj_matrix[i][j] = adj_matrix[i][j] or (adj_matrix[i][k] and adj_matrix[k][j])
result = []
for u, v in queries:
result.append(adj_matrix[u][v])
print(result)
# Example Usage (replace with actual input when running the solution):
# Simulate input using stdin
import io
import sys
# Example 1
input_str1 = """
2
[[1,0]]
[[0,1],[1,0]]
"""
# Example 2
input_str2 = """
2
[]
[[1,0],[0,1]]
"""
# Example 3
input_str3 = """
3
[[1,2],[1,0],[2,0]]
[[1,0],[1,2]]
"""
# Run for example 1
sys.stdin = io.StringIO(input_str1)
solve()
# Run for example 2
sys.stdin = io.StringIO(input_str2)
solve()
# Run for example 3
sys.stdin = io.StringIO(input_str3)
solve()