Skip to content

Commit 72b5b8e

Browse files
authored
Create 2684. Maximum Number of Moves in a Grid (#620)
2 parents 14f513e + bae111a commit 72b5b8e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int maxMoves(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
int m = grid[0].size();
6+
7+
vector<vector<int>> dp(n, vector<int>(m, 0));
8+
for(int i = 0; i<n;i++) {
9+
dp[i][m-1] = 1;
10+
}
11+
for (int j = m - 2; j >= 0; j--) {
12+
for (int i = 0; i < n; i++) {
13+
int maxMoves = 0;
14+
if (i - 1 >= 0 && grid[i - 1][j + 1] > grid[i][j]) {
15+
maxMoves = max(maxMoves,dp[i - 1][j + 1]);
16+
}
17+
if (grid[i][j + 1] > grid[i][j]) {
18+
maxMoves = max(maxMoves, dp[i][j + 1]);
19+
}
20+
if (i + 1 < n && grid[i + 1][j + 1] > grid[i][j]) {
21+
maxMoves = max(maxMoves, dp[i + 1][j + 1]);
22+
}
23+
dp[i][j] = 1 + maxMoves;
24+
}
25+
}
26+
27+
int maxi = 0;
28+
for (int i = 0; i < n; i++) {
29+
maxi = max(maxi, dp[i][0]);
30+
}
31+
return maxi - 1;
32+
}
33+
};

0 commit comments

Comments
 (0)