<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, prerequisites):
        adj_list = defaultdict(list)
        cache = {}

        # step 1
        for course, prereq in prerequisites:
            adj_list[prereq].append(course)
        
        # step 2 - traversal
        def helper(prereq, visited):
            if prereq in visited:
                return False
            if prereq in cache:
                return cache[prereq]
            visited.add(prereq)
            """
             This is a weird Pythonic thing with defaultdicts -
                if the prereq isn't present, the defaultdict 
                would add the key with an empty list by default 
                which changes the size of our default dict during iteration
            """
            courses = []
            if prereq in adj_list:
                courses = adj_list[prereq]
            for course in courses:
                if not helper(course, visited):
                    cache[course] = False
                    return False
            visited.remove(prereq)
            cache[prereq] = True
            return True

        # step 2 - course iteration
        for prereq, _ in adj_list.items():
            if not helper(prereq, set()):
                return False
        return True
        # adj = [[] for _ in range(numCourses)]
        # # how many incoming edges each course has
        # indegree = [0] * numCourses
        # # topological order of the course
        # ans = []

        # # building the graph
        # for pair in prerequisites:
        #     course = pair[0]
        #     prerequisite = pair[1]
        #     adj[prerequisite].append(course)
        #     # the course has one more prerequisite
        #     indegree[course] += 1

        # # nodes to visit
        # queue = deque()

        # for i in range(numCourses):
        #     # no dependencies
        #     if indegree[i] == 0:
        #         queue.append(i)

        # while queue:
        #     current = queue.popleft()
        #     ans.append(current)

        #     for next_course in adj[current]:
        #         indegree[next_course] -= 1
        #         if indegree[next_course] == 0:
        #             queue.append(next_course)

        # return len(ans) == numCourses

        # indegree = [0] * numCourses
        # adj = [[] for _ in range(numCourses)]

        # for prerequisite in prerequisites:
        #     adj[prerequisite[1]].append(prerequisite[0])
        #     indegree[prerequisite[0]] += 1

        # queue = deque()
        # for i in range(numCourses):
        #     if indegree[i] == 0:
        #         queue.append(i)

        # nodesVisited = 0
        # while queue:
        #     node = queue.popleft()
        #     nodesVisited += 1

        #     for neighbor in adj[node]:
        #         indegree[neighbor] -= 1
        #         if indegree[neighbor] == 0:
        #             queue.append(neighbor)

        # return nodesVisited == numCourses

18600