题目
给定一个 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix
题解
首先很自然的想到使用dfs求解,但写完后发现超时,所以需要进一步考虑如何优化时间
发现每个格子的数字的最长递增路径是固定的,我们可以求解每个格子之后对结果进行记录,采取记忆化dfs的方式,之后经过记录过的格子无需dfs可直接取得结果。
实现
未采取记忆化的dfs
class Solution {
private static final int[][] DIRECTIONS = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
private int[][] matrix;
private int rows, cols;
public int longestIncreasingPath(int[][] matrix) {
this.rows = matrix.length;
if (rows == 0) return 0;
this.cols = matrix[0].length;
if (cols == 0) return 0;
this.matrix = matrix;
int max = Integer.MIN_VALUE;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
max = Math.max(max, dfs(i, j, 0));
}
}
return max;
}
private int dfs(int x, int y, int sum) {
sum++;
int max = Integer.MIN_VALUE;
boolean flag = false;
for (int[] direction: DIRECTIONS) {
int newX = x + direction[0];
int newY = y + direction[1];
if (inMap(newX, newY) && matrix[newX][newY] > matrix[x][y]) {
flag = true;
max = Math.max(max, dfs(newX, newY, sum));
}
}
if (flag) return max;
return sum;
}
private boolean inMap(int x, int y) {
return x >= 0 && x < rows && y >= 0 && y <cols;
}
}
记忆化dfs (自解)
class Solution {
private static final int[][] DIRECTIONS = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
private int[][] matrix;
private int[][] marked;
private int rows, cols;
public int longestIncreasingPath(int[][] matrix) {
this.rows = matrix.length;
if (rows == 0) return 0;
this.cols = matrix[0].length;
if (cols == 0) return 0;
this.matrix = matrix;
this.marked = new int[rows][cols];
int max = Integer.MIN_VALUE;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
max = Math.max(max, dfs(i, j));
}
}
return max;
}
private int dfs(int x, int y) {
int res = 1;
int max = 0;
for (int[] direction: DIRECTIONS) {
int newX = x + direction[0];
int newY = y + direction[1];
if (inMap(newX, newY) && matrix[newX][newY] > matrix[x][y]) {
int t;
if (marked[newX][newY] != 0) {
t = marked[newX][newY];
}else {
t = dfs(newX, newY);
}
max = Math.max(max, t);
}
}
res += max;
marked[x][y] = res;
return res;
}
private boolean inMap(int x, int y) {
return x >= 0 && x < rows && y >= 0 && y <cols;
}
}
记忆化dfs (代码优化)
class Solution {
private static final int[][] DIRECTIONS = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
private int[][] matrix;
private int[][] marked;
private int rows, cols;
public int longestIncreasingPath(int[][] matrix) {
this.rows = matrix.length;
if (rows == 0) return 0;
this.cols = matrix[0].length;
if (cols == 0) return 0;
this.matrix = matrix;
this.marked = new int[rows][cols];
int max = Integer.MIN_VALUE;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
max = Math.max(max, dfs(i, j));
}
}
return max;
}
private int dfs(int x, int y) {
if (marked[x][y] != 0) {
return marked[x][y];
}
marked[x][y] = 1;
for (int[] direction: DIRECTIONS) {
int newX = x + direction[0];
int newY = y + direction[1];
if (inMap(newX, newY) && matrix[newX][newY] > matrix[x][y]) {
marked[x][y] = Math.max(marked[x][y], dfs(newX, newY) + 1);
}
}
return marked[x][y];
}
private boolean inMap(int x, int y) {
return x >= 0 && x < rows && y >= 0 && y <cols;
}
}