<p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p>

<ul>
	<li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li>
</ul>

<p>Return <code>true</code> if you can finish all courses. Otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
<strong>Output:</strong> true
<strong>Explanation:</strong> There are a total of 2 courses to take. 
To take course 1 you should have finished course 0. So it is possible.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> There are a total of 2 courses to take. 
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= numCourses &lt;= 2000</code></li>
	<li><code>0 &lt;= prerequisites.length &lt;= 5000</code></li>
	<li><code>prerequisites[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; numCourses</code></li>
	<li>All the pairs prerequisites[i] are <strong>unique</strong>.</li>
</ul>

"""

"""
class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        # Create a list to track how many prerequisites each course has (indegree)
        prerequisites_count: list[int] = [0] * numCourses
        
        # Create a list to track which courses depend on each course (adjacency list)
        course_dependents: list[list[int]] = [[] for _ in range(numCourses)]

        # Populate the prerequisites_count and course_dependents lists based on the prerequisites
        for course, prerequisite in prerequisites:  # prerequisite -> course
            prerequisites_count[course] += 1  # Increase indegree for the course
            course_dependents[prerequisite].append(course)  # Add course to dependents of prerequisite

        # Initialize a queue to store courses that can be taken (those with no prerequisites)
        ready_courses = []
        courses_completed = 0  # Keep track of how many courses we can complete

        # Find all courses with no prerequisites and add them to the queue
        for course in range(numCourses):
            if prerequisites_count[course] == 0:  # If a course has no prerequisites
                ready_courses.append(course)

        # Process the courses in topological order
        while ready_courses:
            current_course = ready_courses.pop()  # Take the course with no prerequisites
            courses_completed += 1  # Count this course as completed

            # For each course that depends on the current course, reduce its prerequisite count
            for dependent_course in course_dependents[current_course]:
                prerequisites_count[dependent_course] -= 1  # Remove one prerequisite
                # If the dependent course now has no prerequisites, add it to the ready_courses
                if prerequisites_count[dependent_course] == 0:
                    ready_courses.append(dependent_course)

        # If we completed all the courses, return True. Otherwise, return False.
        return courses_completed == numCourses





18100