class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
}
};
剑指 Offer II 112. 最长递增路径
给定一个 m x n
整数矩阵 matrix
,找出其中 最长递增路径 的长度。
对于每个单元格,你可以往上,下,左,右四个方向移动。 不能 在 对角线 方向上移动或移动到 边界外(即不允许环绕)。
示例 1:
输入:matrix = [[9,9,4],[6,6,8],[2,1,1]]
输出:4
解释:最长递增路径为 [1, 2, 6, 9]
。
示例 2:
输入:matrix = [[3,4,5],[3,2,6],[2,2,1]]
输出:4
解释:最长递增路径是 [3, 4, 5, 6]
。注意不允许在对角线方向上移动。
示例 3:
输入:matrix = [[1]] 输出:1
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
0 <= matrix[i][j] <= 231 - 1
注意:本题与主站 329 题相同: https://leetcode.cn/problems/longest-increasing-path-in-a-matrix/
原站题解
java 解法, 执行用时: 15 ms, 内存消耗: 41.8 MB, 提交时间: 2023-04-22 12:29:05
class Solution { public int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public int rows, columns; public int longestIncreasingPath(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return 0; } rows = matrix.length; columns = matrix[0].length; int[][] outdegrees = new int[rows][columns]; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { for (int[] dir : dirs) { int newRow = i + dir[0], newColumn = j + dir[1]; if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[i][j]) { ++outdegrees[i][j]; } } } } Queue<int[]> queue = new LinkedList<int[]>(); for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { if (outdegrees[i][j] == 0) { queue.offer(new int[]{i, j}); } } } int ans = 0; while (!queue.isEmpty()) { ++ans; int size = queue.size(); for (int i = 0; i < size; ++i) { int[] cell = queue.poll(); int row = cell[0], column = cell[1]; for (int[] dir : dirs) { int newRow = row + dir[0], newColumn = column + dir[1]; if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] < matrix[row][column]) { --outdegrees[newRow][newColumn]; if (outdegrees[newRow][newColumn] == 0) { queue.offer(new int[]{newRow, newColumn}); } } } } } return ans; } }
python3 解法, 执行用时: 280 ms, 内存消耗: 16 MB, 提交时间: 2023-04-22 12:28:42
# 拓扑排序 class Solution: DIRS = [(-1, 0), (1, 0), (0, -1), (0, 1)] def longestIncreasingPath(self, matrix: List[List[int]]) -> int: if not matrix: return 0 rows, columns = len(matrix), len(matrix[0]) outdegrees = [[0] * columns for _ in range(rows)] queue = collections.deque() for i in range(rows): for j in range(columns): for dx, dy in Solution.DIRS: newRow, newColumn = i + dx, j + dy if 0 <= newRow < rows and 0 <= newColumn < columns and matrix[newRow][newColumn] > matrix[i][j]: outdegrees[i][j] += 1 if outdegrees[i][j] == 0: queue.append((i, j)) ans = 0 while queue: ans += 1 size = len(queue) for _ in range(size): row, column = queue.popleft() for dx, dy in Solution.DIRS: newRow, newColumn = row + dx, column + dy if 0 <= newRow < rows and 0 <= newColumn < columns and matrix[newRow][newColumn] < matrix[row][column]: outdegrees[newRow][newColumn] -= 1 if outdegrees[newRow][newColumn] == 0: queue.append((newRow, newColumn)) return ans
golang 解法, 执行用时: 36 ms, 内存消耗: 7.3 MB, 提交时间: 2023-04-22 12:28:14
var ( dirs = [][]int{[]int{-1, 0}, []int{1, 0}, []int{0, -1}, []int{0, 1}} rows, columns int ) func longestIncreasingPath(matrix [][]int) int { if len(matrix) == 0 || len(matrix[0]) == 0 { return 0 } rows, columns = len(matrix), len(matrix[0]) outdegrees := make([][]int, rows) for i := 0; i < rows; i++ { outdegrees[i] = make([]int, columns) } for i := 0; i < rows; i++ { for j := 0; j < columns; j++ { for _, dir := range dirs { newRow, newColumn := i + dir[0], j + dir[1] if newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[i][j] { outdegrees[i][j]++ } } } } queue := [][]int{} for i := 0; i < rows; i++ { for j := 0; j < columns; j++ { if outdegrees[i][j] == 0 { queue = append(queue, []int{i, j}) } } } ans := 0 for len(queue) != 0 { ans++ size := len(queue) for i := 0; i < size; i++ { cell := queue[0] queue = queue[1:] row, column := cell[0], cell[1] for _, dir := range dirs { newRow, newColumn := row + dir[0], column + dir[1] if newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] < matrix[row][column] { outdegrees[newRow][newColumn]-- if outdegrees[newRow][newColumn] == 0 { queue = append(queue, []int{newRow, newColumn}) } } } } } return ans }
golang 解法, 执行用时: 24 ms, 内存消耗: 6.7 MB, 提交时间: 2023-04-22 12:27:53
var ( dirs = [][]int{[]int{-1, 0}, []int{1, 0}, []int{0, -1}, []int{0, 1}} rows, columns int ) func longestIncreasingPath(matrix [][]int) int { if len(matrix) == 0 || len(matrix[0]) == 0 { return 0 } rows, columns = len(matrix), len(matrix[0]) memo := make([][]int, rows) for i := 0; i < rows; i++ { memo[i] = make([]int, columns) } ans := 0 for i := 0; i < rows; i++ { for j := 0; j < columns; j++ { ans = max(ans, dfs(matrix, i, j, memo)) } } return ans } func dfs(matrix [][]int, row, column int, memo [][]int) int { if memo[row][column] != 0 { return memo[row][column] } memo[row][column]++ for _, dir := range dirs { newRow, newColumn := row + dir[0], column + dir[1] if newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[row][column] { memo[row][column] = max(memo[row][column], dfs(matrix, newRow, newColumn, memo) + 1) } } return memo[row][column] } func max(x, y int) int { if x > y { return x } return y }
python3 解法, 执行用时: 272 ms, 内存消耗: 19.1 MB, 提交时间: 2023-04-22 12:27:40
class Solution: DIRS = [(-1, 0), (1, 0), (0, -1), (0, 1)] def longestIncreasingPath(self, matrix: List[List[int]]) -> int: if not matrix: return 0 @lru_cache(None) def dfs(row: int, column: int) -> int: best = 1 for dx, dy in Solution.DIRS: newRow, newColumn = row + dx, column + dy if 0 <= newRow < rows and 0 <= newColumn < columns and matrix[newRow][newColumn] > matrix[row][column]: best = max(best, dfs(newRow, newColumn) + 1) return best ans = 0 rows, columns = len(matrix), len(matrix[0]) for i in range(rows): for j in range(columns): ans = max(ans, dfs(i, j)) return ans
java 解法, 执行用时: 8 ms, 内存消耗: 41.6 MB, 提交时间: 2023-04-22 12:27:05
// 记忆化深度优先搜索 class Solution { public int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public int rows, columns; public int longestIncreasingPath(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return 0; } rows = matrix.length; columns = matrix[0].length; int[][] memo = new int[rows][columns]; int ans = 0; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { ans = Math.max(ans, dfs(matrix, i, j, memo)); } } return ans; } public int dfs(int[][] matrix, int row, int column, int[][] memo) { if (memo[row][column] != 0) { return memo[row][column]; } ++memo[row][column]; for (int[] dir : dirs) { int newRow = row + dir[0], newColumn = column + dir[1]; if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[row][column]) { memo[row][column] = Math.max(memo[row][column], dfs(matrix, newRow, newColumn, memo) + 1); } } return memo[row][column]; } }