2684.cpp (729B)
1 class Solution { 2 public: 3 int maxMoves(const vector<vector<int>> &grid) const { 4 const int n = size(grid), m = size(grid[0]); 5 static int dp[1001]; 6 7 memset(dp, 0x00, sizeof(dp)); 8 for (int j = m - 2; j >= 0; j--) { 9 for (int i = 0, prev; i < n; i++) { 10 int crnt = 0; 11 if (i > 0 && grid[i][j] < grid[i - 1][j + 1]) crnt = max(crnt, prev + 1); 12 if (grid[i][j] < grid[i][j + 1]) crnt = max(crnt, dp[i] + 1); 13 if (i < n - 1 && grid[i][j] < grid[i + 1][j + 1]) crnt = max(crnt, dp[i + 1] + 1); 14 prev = dp[i]; 15 dp[i] = crnt; 16 } 17 } 18 19 return *max_element(dp, dp + n); 20 } 21 };