-
Notifications
You must be signed in to change notification settings - Fork 521
/
Copy path10. Longest Increasing Path in a Matrix.cpp
70 lines (58 loc) · 1.7 KB
/
10. Longest Increasing Path in a Matrix.cpp
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
/*
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 1:
Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].
Example 2:
Input: matrix = [[3,4,5],[3,2,6],[2,2,1]]
Output: 4
Explanation: The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
Example 3:
Input: matrix = [[1]]
Output: 1
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
0 <= matrix[i][j] <= 231 - 1
*/
class Solution
{
public:
int dirs[4][4] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int dfs(vector<vector<int>> &mat, int r, int c, vector<vector<int>> &memo)
{
if (memo[r][c] != -1)
return memo[r][c];
int n = mat.size(), m = mat[0].size();
int ans = 0;
for (auto &dir : dirs)
{
int nextR = r + dir[0], nextC = c + dir[1];
if (nextR < 0 || nextC < 0 || nextR >= n || nextC >= m || mat[r][c] >= mat[nextR][nextC])
continue;
ans = max(ans, dfs(mat, nextR, nextC, memo));
}
memo[r][c] = ans + 1;
return memo[r][c];
}
int longestIncreasingPath(vector<vector<int>> &matrix)
{
int n = matrix.size(), m = matrix[0].size();
vector<vector<int>> memo(n + 1, vector<int>(m + 1, -1));
int ans = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
int curr_ans = dfs(matrix, i, j, memo);
ans = max(ans, curr_ans);
}
}
return ans;
}
};