-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0329_longestIncreasingPath.cc
80 lines (73 loc) · 1.77 KB
/
Problem_0329_longestIncreasingPath.cc
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
71
72
73
74
75
76
77
78
79
80
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int longestIncreasingPath(vector<vector<int>>& matrix)
{
if (matrix.size() == 0 || matrix[0].size() == 0)
{
return 0;
}
int ans = 0;
int N = matrix.size();
int M = matrix[0].size();
vector<vector<int>> dp(N, vector<int>(M));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
ans = std::max(ans, lip(matrix, i, j, dp));
}
}
return ans;
}
int lip(vector<vector<int>>& matrix, int i, int j, vector<vector<int>>& dp)
{
if (dp[i][j] != 0)
{
return dp[i][j];
}
int next = 0;
if (canWalk(matrix, i, j, i - 1, j))
{
next = std::max(next, lip(matrix, i - 1, j, dp));
}
if (canWalk(matrix, i, j, i + 1, j))
{
next = std::max(next, lip(matrix, i + 1, j, dp));
}
if (canWalk(matrix, i, j, i, j - 1))
{
next = std::max(next, lip(matrix, i, j - 1, dp));
}
if (canWalk(matrix, i, j, i, j + 1))
{
next = std::max(next, lip(matrix, i, j + 1, dp));
}
dp[i][j] = 1 + next;
return dp[i][j];
}
bool canWalk(vector<vector<int>>& matrix, int i1, int j1, int i2, int j2)
{
return i2 >= 0 && i2 < matrix.size() && j2 >= 0 && j2 < matrix[0].size() &&
matrix[i1][j1] < matrix[i2][j2];
}
};
void testLongestIncreasingPath()
{
Solution s;
vector<vector<int>> m1 = {{9, 9, 4}, {6, 6, 8}, {2, 1, 1}};
vector<vector<int>> m2 = {{3, 4, 5}, {3, 2, 6}, {2, 2, 1}};
vector<vector<int>> m3 = {{1}};
EXPECT_EQ_INT(4, s.longestIncreasingPath(m1));
EXPECT_EQ_INT(4, s.longestIncreasingPath(m2));
EXPECT_EQ_INT(1, s.longestIncreasingPath(m3));
EXPECT_SUMMARY;
}
int main()
{
testLongestIncreasingPath();
return 0;
}