java.lang.Object
g2601_2700.s2684_maximum_number_of_moves_in_a_grid.Solution

public class Solution extends Object
2684 - Maximum Number of Moves in a Grid.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> <code>m x n</code> matrix <code>grid</code> consisting of <strong>positive</strong> integers.</p> <p>You can start at <strong>any</strong> cell in the first column of the matrix, and traverse the grid in the following way:</p> <ul> <li>From a cell <code>(row, col)</code>, you can move to any of the cells: <code>(row - 1, col + 1)</code>, <code>(row, col + 1)</code> and <code>(row + 1, col + 1)</code> such that the value of the cell you move to, should be <strong>strictly</strong> bigger than the value of the current cell.</li> </ul> <p>Return <em>the <strong>maximum</strong> number of <strong>moves</strong> that you can perform.</em></p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png" alt="" /></p> <p><strong>Input:</strong> grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> We can start at the cell (0, 0) and make the following moves:</p> <ul> <li>(0, 0) -> (0, 1).</li> <li>(0, 1) -> (1, 2).</li> <li>(1, 2) -> (2, 3).</li> </ul> <p>It can be shown that it is the maximum number of moves that can be made.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/04/12/yetgrid4drawio.png" alt="" /> <strong>Input:</strong> grid = [[3,2,4],[2,1,9],[1,1,7]]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> Starting from any cell in the first column we cannot perform any moves.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>2 <= m, n <= 1000</code></li> <li><code>4 <= m * n <= 10<sup>5</sup></code></li> <li><code>1 <= grid[i][j] <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxMoves

      public int maxMoves(int[][] grid)