|
55 | 55 |
|
56 | 56 | <!-- 这里可写通用的实现逻辑 -->
|
57 | 57 |
|
| 58 | +记忆化搜索。 |
| 59 | + |
58 | 60 | <!-- tabs:start -->
|
59 | 61 |
|
60 | 62 | ### **Python3**
|
61 | 63 |
|
62 | 64 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
63 | 65 |
|
64 | 66 | ```python
|
65 |
| - |
| 67 | +class Solution: |
| 68 | + def longestIncreasingPath(self, matrix: List[List[int]]) -> int: |
| 69 | + @lru_cache(None) |
| 70 | + def dfs(i, j): |
| 71 | + ans = 1 |
| 72 | + for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]: |
| 73 | + x, y = i + a, j + b |
| 74 | + if 0 <= x < m and 0 <= y < n and matrix[x][y] > matrix[i][j]: |
| 75 | + ans = max(ans, dfs(x, y) + 1) |
| 76 | + return ans |
| 77 | + |
| 78 | + ans = 0 |
| 79 | + m, n = len(matrix), len(matrix[0]) |
| 80 | + for i in range(m): |
| 81 | + for j in range(n): |
| 82 | + ans = max(ans, dfs(i, j)) |
| 83 | + return ans |
66 | 84 | ```
|
67 | 85 |
|
68 | 86 | ### **Java**
|
69 | 87 |
|
70 | 88 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
71 | 89 |
|
72 | 90 | ```java
|
| 91 | +class Solution { |
| 92 | + |
| 93 | + private int[][] memo; |
| 94 | + private int[][] matrix; |
| 95 | + private int m; |
| 96 | + private int n; |
| 97 | + |
| 98 | + public int longestIncreasingPath(int[][] matrix) { |
| 99 | + this.matrix = matrix; |
| 100 | + m = matrix.length; |
| 101 | + n = matrix[0].length; |
| 102 | + memo = new int[m][n]; |
| 103 | + for (int i = 0; i < m; ++i) { |
| 104 | + Arrays.fill(memo[i], -1); |
| 105 | + } |
| 106 | + int ans = 0; |
| 107 | + for (int i = 0; i < m; ++i) { |
| 108 | + for (int j = 0; j < n; ++j) { |
| 109 | + ans = Math.max(ans, dfs(i, j)); |
| 110 | + } |
| 111 | + } |
| 112 | + return ans; |
| 113 | + } |
| 114 | + |
| 115 | + private int dfs(int i, int j) { |
| 116 | + if (memo[i][j] != -1) { |
| 117 | + return memo[i][j]; |
| 118 | + } |
| 119 | + int ans = 1; |
| 120 | + int[][] dirs = { { 0, -1 }, { 0, 1 }, { 1, 0 }, { -1, 0 } }; |
| 121 | + for (int[] dir : dirs) { |
| 122 | + int x = i + dir[0], y = j + dir[1]; |
| 123 | + if ( |
| 124 | + x >= 0 && |
| 125 | + x < m && |
| 126 | + y >= 0 && |
| 127 | + y < n && |
| 128 | + matrix[x][y] > matrix[i][j] |
| 129 | + ) { |
| 130 | + ans = Math.max(ans, dfs(x, y) + 1); |
| 131 | + } |
| 132 | + } |
| 133 | + memo[i][j] = ans; |
| 134 | + return ans; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +``` |
| 139 | + |
| 140 | +### **C++** |
| 141 | + |
| 142 | +```cpp |
| 143 | +class Solution { |
| 144 | +public: |
| 145 | + vector<vector<int>> memo; |
| 146 | + vector<vector<int>> matrix; |
| 147 | + int m; |
| 148 | + int n; |
| 149 | + |
| 150 | + int longestIncreasingPath(vector<vector<int>>& matrix) { |
| 151 | + m = matrix.size(); |
| 152 | + n = matrix[0].size(); |
| 153 | + memo.resize(m, vector<int>(n, -1)); |
| 154 | + this->matrix = matrix; |
| 155 | + int ans = 0; |
| 156 | + for (int i = 0; i < m; ++i) |
| 157 | + for (int j = 0; j < n; ++j) |
| 158 | + ans = max(ans, dfs(i, j)); |
| 159 | + return ans; |
| 160 | + } |
| 161 | + |
| 162 | + int dfs(int i, int j) { |
| 163 | + if (memo[i][j] != -1) return memo[i][j]; |
| 164 | + int ans = 1; |
| 165 | + vector<vector<int>> dirs = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; |
| 166 | + for (auto& dir : dirs) |
| 167 | + { |
| 168 | + int x = i + dir[0], y = j + dir[1]; |
| 169 | + if (x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]) |
| 170 | + ans = max(ans, dfs(x, y) + 1); |
| 171 | + } |
| 172 | + memo[i][j] = ans; |
| 173 | + return ans; |
| 174 | + } |
| 175 | +}; |
| 176 | +``` |
73 | 177 |
|
| 178 | +### **Go** |
| 179 | +
|
| 180 | +```go |
| 181 | +func longestIncreasingPath(matrix [][]int) int { |
| 182 | + m, n := len(matrix), len(matrix[0]) |
| 183 | + memo := make([][]int, m) |
| 184 | + for i := range memo { |
| 185 | + memo[i] = make([]int, n) |
| 186 | + for j := range memo[i] { |
| 187 | + memo[i][j] = -1 |
| 188 | + } |
| 189 | + } |
| 190 | + ans := -1 |
| 191 | + var dfs func(i, j int) int |
| 192 | + dfs = func(i, j int) int { |
| 193 | + if memo[i][j] != -1 { |
| 194 | + return memo[i][j] |
| 195 | + } |
| 196 | + ans := 1 |
| 197 | + dirs := [4][2]int{{-1, 0}, {1, 0}, {0, 1}, {0, -1}} |
| 198 | + for _, dir := range dirs { |
| 199 | + x, y := i+dir[0], j+dir[1] |
| 200 | + if x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j] { |
| 201 | + ans = max(ans, dfs(x, y)+1) |
| 202 | + } |
| 203 | + } |
| 204 | + memo[i][j] = ans |
| 205 | + return ans |
| 206 | + } |
| 207 | + for i := 0; i < m; i++ { |
| 208 | + for j := 0; j < n; j++ { |
| 209 | + ans = max(ans, dfs(i, j)) |
| 210 | + } |
| 211 | + } |
| 212 | + return ans |
| 213 | +} |
| 214 | +
|
| 215 | +func max(a, b int) int { |
| 216 | + if a > b { |
| 217 | + return a |
| 218 | + } |
| 219 | + return b |
| 220 | +} |
74 | 221 | ```
|
75 | 222 |
|
76 | 223 | ### **...**
|
|
0 commit comments