[LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization

时间:2022-02-15 21:29:01

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:

Input: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].

Example 2:

Input: nums =
[
[3,4,5],
[3,2,6],
[2,2,1]
]
Output: 4
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

这个题目我理解为还是Dynamic Programming, 虽然说是加入了memoization, 但是DP的本质不本来也是memoization么? anyways, 这个题目就是用DFS, 但是每次的结果我们会存

在dp数组里面, 另外有个visited set, 去标记我们是否已经visited过了, 已经得到这个元素的值, 如果是的话直接返回, 节省time,

另外 function的话就是A[i][j] = max(A[i][j], helper(neig) + 1), 需要注意的是dp数组存的是以该点作为结束的点的最大值, 那么我们就需要向小的值去search, 所以有

matrix[i][j] > matrix[nr][nc] 的判断在那里.

1. Constraints

1) empty  , return 0

2) element will be interger, 有duplicates, 但是increasing 是绝对的, 所以不用担心duplicates

2. Ideas

memoization DFS,    T: O(m*n)   S: O(m*n)

1) edge case

2) helper dfs function, 向4个邻居方向search, max(A[i][j], helper(neig) + 1), 最开始的时候加判断, 如果flag, 直接返回dp[i][j]

3) for lr, for lc, ans = max(ans, helper(i,j))

4) return ans

3. code

class Solution:
def longestIncreasePath(self, matrix):
if not matrix or not matrix[0]: return 0
lrc, ans, dirs = [len(matrix), len(matrix[0])], 0, [(1,0), (-1, 0), (0,1), (0,-1)]
dp, visited = [[1]*lrc[1] for _ in range(lrc[0])] , set()
def helper(i, j):
if (i, j) in visited:
return dp[i][j]
       visited.add((i, j))
for c1, c2 in dirs:
nr, nc = c1 + i, c2 + j
if 0 <= nr < lrc[0] and 0 <= nc < lrc[1] and matrix[i][j] > matrix[nr][nc]:
dp[i][j] = max(dp[i][j], helper(nr, nc) + 1)
return dp[i][j]
for i in range(lrc[0]):
for j in range(lrc[1]):
ans = max(ans, helper(i,j))
return ans

4. Test cases

[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4