-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.h
48 lines (42 loc) · 1.35 KB
/
solution.h
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
vector<piii> V;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
V.push_back({matrix[i][j], {i, j}});
}
}
sort(V.begin(), V.end());
int d[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int ans = 0;
vector<vector<int>> dp(n, vector<int>(m, 0));
for (int i = 0; i < V.size(); i++) {
int val = V[i].first;
int x = V[i].second.first, y = V[i].second.second;
dp[x][y] = 1;
for (int j = 0; j < 4; j++) {
int x2 = x + d[j][0], y2 = y + d[j][1];
if (x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && matrix[x2][y2] < matrix[x][y]) {
dp[x][y] = max(dp[x][y], dp[x2][y2] + 1);
}
}
ans = max(ans, dp[x][y]);
}
return ans;
}
};