-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2684.cpp
More file actions
28 lines (28 loc) · 891 Bytes
/
2684.cpp
File metadata and controls
28 lines (28 loc) · 891 Bytes
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
class Solution {
public:
int maxMoves(vector<vector<int>>& grid) {
int maxPath = 0;
int m = grid.size();
int n = grid[0].size();
vector<int> dp(m, 0);
vector<int> preDp(m, 0);
int res = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < m; ++j) {
dp[j] = 0;
if (j - 1 >= 0 && grid[j][i] > grid[j - 1][i - 1]) {
dp[j] = max(dp[j], preDp[j - 1] + 1);
}
if (grid[j][i] > grid[j][i - 1]) {
dp[j] = max(dp[j], preDp[j] + 1);
}
if (j + 1 < m && grid[j][i] > grid[j + 1][i - 1]) {
dp[j] = max(dp[j], preDp[j + 1] + 1);
}
if (dp[j] == i) res = max(res, dp[j]);
}
preDp = dp;
}
return res;
}
};