Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
Example 1:
nums = [
[9,9,4],
[6,6,8],
[2,1,1]
]
Return 4 The longest increasing path is [1, 2, 6, 9].
Example 2:
nums = [
[3,4,5],
[3,2,6],
[2,2,1]
]
Return 4 The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
deflongestIncreasingPath(self, matrix: List[List[int]]) -> int: ary = [] for i in range(len(matrix)): for j in range(len(matrix[0])): ary.append((matrix[i][j], i, j)) ary.sort() dp = [[1for _ in range(len(matrix[0]))] for _ in range(len(matrix))] for val, _x, _y in ary: for _dx, _dy in OFFSETS: x, y = _x + _dx, _y + _dy if x < 0or x >= len(matrix) or y < 0or y >= len(matrix[0]): continue if matrix[x][y] > matrix[_x][_y]: dp[x][y] = max(dp[x][y], dp[_x][_y] + 1) return max(map(max, dp))