Skip to content

Commit 5393612

Browse files
committed
add
1 parent ad7c912 commit 5393612

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

extra/longestincpath.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def longestIncreasingPath(self, matrix):
3+
m, n = len(matrix), len(matrix[0])
4+
directions = [(0, -1), (0, 1), (1, 0), (-1, 0)]
5+
6+
@lru_cache(None)
7+
def dfs(x, y):
8+
ans = 1
9+
for dx, dy in directions:
10+
if 0 <= x + dx < m and 0 <= y + dy < n and matrix[x + dx][y + dy] < matrix[x][y]:
11+
ans = max(ans, dfs(x + dx, y + dy) + 1)
12+
return ans
13+
14+
return max(dfs(i, j) for i, j in product(range(m), range(n)))

0 commit comments

Comments
 (0)