<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>

# 2024/09/15
# 面试前刷Doordash高频题
#
# 这题有点意思，相当于longest increasing subset的二维变种
# 能否用DP，关键问题
# 应该没问题，首先不会有使用同一个元素的情况，因为是严格increasing
# 接下去问题是如何填表了，如果用Bottom up的话
# 感觉这题用top down更合适，不用考虑填表问题
# 那就DFS，这里问题是去重怎么弄？想起来应该是之前有讨论DFS怎么detect cycle，需要有一个visited表示当前这个path的parents，和传统的DFS的visited有点不一样
class Solution:
    def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
        memo = {}
        global_max = [0]

        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if (i, j) not in memo:
                    visited = set()
                    visited.add((i, j))
                    self.helper(i, j, visited, memo, matrix, global_max)

        return global_max[0]

    def helper(self, row, col, visited, memo, matrix, global_max):
        if (row, col) in memo:
            return memo[(row, col)]

        local_max = 1
        for dx, dy in [1, 0], [-1, 0], [0, 1], [0, -1]:
            x = row + dx
            y = col + dy
            if 0 <= x < len(matrix) and 0 <= y < len(matrix[0]):
                if (x, y) in visited:
                    continue
                
                if matrix[x][y] < matrix[row][col]:
                    visited.add((x, y))
                    local_max = max(local_max, 1 + self.helper(x, y, visited, memo, matrix, global_max))
                    visited.remove((x, y))

        memo[(row, col)] = local_max
        global_max[0] = max(global_max[0], local_max)

        return local_max
18335