<p>Given an <code>m x n</code> integers <code>matrix</code>, return <em>the length of the longest increasing path in </em><code>matrix</code>.</p>

<p>From each cell, you can either move in four directions: left, right, up, or down. You <strong>may not</strong> move <strong>diagonally</strong> or move <strong>outside the boundary</strong> (i.e., wrap-around is not allowed).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/05/grid1.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> matrix = [[9,9,4],[6,6,8],[2,1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest increasing path is <code>[1, 2, 6, 9]</code>.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/27/tmp-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> matrix = [[3,4,5],[3,2,6],[2,2,1]]
<strong>Output:</strong> 4
<strong>Explanation: </strong>The longest increasing path is <code>[3, 4, 5, 6]</code>. Moving diagonally is not allowed.
</pre>

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

<pre>
<strong>Input:</strong> matrix = [[1]]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= matrix[i][j] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

r'''
# Requirements

m = len(matrix), n = len(matrix[i])
1 <= m, n <= 200
0 <= matrix[i] <= 2^31 - 1

# Edge cases

1. No increasing paths

# Solutions

1. Brute force, backtracking
    - O(mn * mn) time, O(mn) auxiliary space
2. Topological sort
    - O(mn) time, O(mn) auxiliary space

9 9 4
6 8 8
2 1 1
'''
DIRECTIONS = [(1, 0), (0, -1), (-1, 0), (0, 1)]
class Solution:
    def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
        rows, cols = len(matrix), len(matrix[0])

        # Construct adjacency lists and count indegrees
        adj_lists = [[[] for _ in range(cols)] for _ in range(rows)]
        indegrees = [[0 for _ in range(cols)] for _ in range(rows)]

        for r in range(rows):
            for c in range(cols):
                for dr, dc in DIRECTIONS:
                    nr, nc = r + dr, c + dc
                    if nr < 0 or nr >= rows or nc < 0 or nc >= cols or matrix[nr][nc] <= matrix[r][c]:
                        continue

                    adj_lists[r][c].append((nr, nc))
                    indegrees[nr][nc] += 1

        # Find longest increasing path
        sources, max_length = deque(), 0

        for r in range(rows):
            for c in range(cols):
                if indegrees[r][c] == 0 and adj_lists[r][c]:
                    sources.append((r, c))

        while sources:
            max_length += 1
            for _ in range(len(sources)):
                r, c = sources.popleft()

                for nr, nc in adj_lists[r][c]:
                    indegrees[nr][nc] -= 1

                    if indegrees[nr][nc] == 0:
                        sources.append((nr, nc))

        return max(max_length, 1)
19505