Description
Longest Increasing Path in a Matrix: Given an m x n
integers matrix
, return the length of the longest increasing path in matrix
.
From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
Example:
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].
Solution
There are two solutions: DFS + Memorization OR BFS + Indegree.
Only DFS or BFS will get TLE, so we must combined with Memorization or Indegree.
For DFS, we access all cells in the matrix: if a cell have been accessed, we remember the longest increasing path it can get for reusing. This is because if we access this cell again, we can just reuse the memorized path.
For BFS, we also must access all cells: if an adjcent value is smaller than the current value, which means the current value CANNOT be the beginning cells. So, we increase its indegree. For those cells which indegrees are 0, which means they are the smallest compared to their adjecent cells, they must be the begining of a path. So, we just put them into queue, then update the indegrees. Finally, we can get the longest increasing path.
DFS + Memorization Code
class Solution {
public:
int m, n;
int xdir[4] = {-1, 0, 1, 0}, ydir[4] = {0, -1, 0, 1};
int dfs(int x, int y, vector<vector<int>>& matrix, vector<vector<int>>& level) {
if(level[x][y] != -1)
return level[x][y];
level[x][y] = 1;
for(int k = 0; k < 4; k++) {
int tmpx = x + xdir[k], tmpy = y + ydir[k];
if(tmpx < 0 || tmpx >= m || tmpy < 0 || tmpy >= n)
continue;
if(matrix[tmpx][tmpy] < matrix[x][y])
level[x][y] = max(level[x][y], dfs(tmpx, tmpy, matrix, level) + 1);
}
return level[x][y];
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
m = matrix.size(), n = matrix[0].size();
vector<vector<int>> level(m, vector<int>(n, -1));
int ans = 1;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
ans = max(ans, level[i][j] = dfs(i, j, matrix, level));
}
}
return ans;
}
};
BFS + Indegree Code
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> inDegree(m, vector<int>(n, 0));
int xdir[4] = {-1, 0, 1, 0}, ydir[4] = {0, -1, 0, 1};
queue<pair<int, int>> q;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < 4; k++) {
int tmpx = i + xdir[k], tmpy = j + ydir[k];
if(tmpx < 0 || tmpx >= m || tmpy < 0 || tmpy >= n || matrix[tmpx][tmpy] >= matrix[i][j])
continue;
inDegree[i][j]++;
}
if(!inDegree[i][j])
q.push({i, j});
}
}
int level = 0;
while(!q.empty()) {
int size = q.size();
while(size--) {
auto p = q.front();
q.pop();
int i = p.first, j = p.second;
for(int k = 0; k < 4; k++) {
int tmpx = i + xdir[k], tmpy = j + ydir[k];
if(tmpx < 0 || tmpx >= m || tmpy < 0 || tmpy >= n || matrix[tmpx][tmpy] <= matrix[i][j])
continue;
inDegree[tmpx][tmpy]--;
if(!inDegree[tmpx][tmpy])
q.push({tmpx, tmpy});
}
}
level++;
}
return level;
}
};
Complexity
Time complexity: O(m ^ n)
Space complexity: O(m ^ n) for extra space