java.lang.Object
g2601_2700.s2617_minimum_number_of_visited_cells_in_a_grid.Solution

public class Solution extends Object
2617 - Minimum Number of Visited Cells in a Grid.<p>Hard</p> <p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p> <p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p> <ul> <li>Cells <code>(i, k)</code> with <code>j < k <= grid[i][j] + j</code> (rightward movement), or</li> <li>Cells <code>(k, j)</code> with <code>i < k <= grid[i][j] + i</code> (downward movement).</li> </ul> <p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" alt="" /></p> <p><strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" alt="" /></p> <p><strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The image above shows one of the paths that visits exactly 3 cells.</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" alt="" /></p> <p><strong>Input:</strong> grid = [[2,1,0],[1,0,0]]</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> It can be proven that no path exists.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 <= m, n <= 10<sup>5</sup></code></li> <li><code>1 <= m * n <= 10<sup>5</sup></code></li> <li><code>0 <= grid[i][j] < m * n</code></li> <li><code>grid[m - 1][n - 1] == 0</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumVisitedCells

      public int minimumVisitedCells(int[][] grid)