-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path329 Longest Increasing Path in a Matrix.py
75 lines (59 loc) · 1.74 KB
/
329 Longest Increasing Path in a Matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
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.
Author: Rajeev Ranjan
"""
class Solution(object):
def __init__(self):
self.cache = None
self.dirs = ((-1, 0), (1, 0), (0, -1), (0, 1),)
def longestIncreasingPath(self, matrix):
"""
dfs + cache
:type matrix: List[List[int]]
:rtype: int
"""
if not matrix: return 0
m, n = len(matrix), len(matrix[0])
self.cache = [[None for _ in xrange(n)] for _ in xrange(m)]
gmax = 1
for i in xrange(m):
for j in xrange(n):
gmax = max(gmax, self.longest(matrix, i, j))
return gmax
def longest(self, matrix, i, j):
"""
Strictly increasing, thus no need to have a visited matrix
"""
if not self.cache[i][j]:
m, n = len(matrix), len(matrix[0])
maxa = 1
for d in self.dirs:
I, J = i + d[0], j + d[1]
if 0 <= I < m and 0 <= J < n and matrix[I][J] > matrix[i][j]:
maxa = max(maxa, 1 + self.longest(matrix, I, J))
self.cache[i][j] = maxa
return self.cache[i][j]
if __name__ == "__main__":
assert Solution().longestIncreasingPath([
[9, 9, 4],
[6, 6, 8],
[2, 1, 1]
]) == 4