Skip to content

update #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions [K]graph/[K]graph-dfs/329-longest-increasing-path-in-a-matrix.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
res = 0
rows, cols = len(matrix), len(matrix[0])
counts = [[- 1 for _ in range(cols)] for _ in range(rows)]
cell_len = {}

def dfs(r, c):
next_count = 0
total = 1
new_total = total
for next_r, next_c in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]:
if next_r not in range(rows) or next_c not in range(cols) or matrix[r][c] >= matrix[next_r][next_c]:
continue
if counts[next_r][next_c] == - 1:
next_count = max(next_count, dfs(next_r, next_c))
if (next_r, next_c) in cell_len:
new_total = max(new_total, total + cell_len[(next_r, next_c)])
else:
next_count = max(next_count, counts[next_r][next_c])
counts[r][c] = 1 + next_count
return counts[r][c]
new_total = max(new_total, total + dfs(next_r, next_c))
cell_len[(r, c)] = new_total
return new_total

for r in range(rows):
for c in range(cols):
res = max(res, dfs(r, c))
return res
dfs(r, c)
return max(cell_len.values())

# time O(RC)
# space O(RC)
Expand Down