TheRealSamuel/LeetCodeProblem
0574
1{2 "id": 2697,3 "name": "minimum_number_of_visited_cells_in_a_grid",4 "difficulty": "Hard",5 "link": "https://leetcode.com/problems/minimum-number-of-visited-cells-in-a-grid/",6 "date": "1680393600000",7 "task_description": "You are given a **0-indexed** `m x n` integer matrix `grid`. Your initial position is at the **top-left** cell `(0, 0)`. Starting from the cell `(i, j)`, you can move to one of the following cells: Cells `(i, k)` with `j < k <= grid[i][j] + j` (rightward movement), or Cells `(k, j)` with `i < k <= grid[i][j] + i` (downward movement). Return _the minimum number of cells you need to visit to reach the **bottom-right** cell_ `(m - 1, n - 1)`. If there is no valid path, return `-1`. **Example 1:** ``` **Input:** grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]] **Output:** 4 **Explanation:** The image above shows one of the paths that visits exactly 4 cells. ``` **Example 2:** ``` **Input:** grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]] **Output:** 3 **Explanation: **The image above shows one of the paths that visits exactly 3 cells. ``` **Example 3:** ``` **Input:** grid = [[2,1,0],[1,0,0]] **Output:** -1 **Explanation:** It can be proven that no path exists. ``` **Constraints:** `m == grid.length` `n == grid[i].length` `1 <= m, n <= 105` `1 <= m * n <= 105` `0 <= grid[i][j] < m * n` `grid[m - 1][n - 1] == 0`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]",12 "output": "4 "13 },14 {15 "label": "Example 2",16 "input": "grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]",17 "output": "3 "18 },19 {20 "label": "Example 3",21 "input": "grid = [[2,1,0],[1,0,0]]",22 "output": "-1 "23 }24 ],25 "private_test_cases": [],26 "haskell_template": "minimumVisitedCells :: [[Int]] -> Int\nminimumVisitedCells grid ",27 "ocaml_template": "let minimumVisitedCells (grid: int list list) : int = ",28 "scala_template": "def minimumVisitedCells(grid: List[List[Int]]): Int = { \n \n}",29 "java_template": "public static int minimumVisitedCells(List<List<Integer>> grid) {\n\n}",30 "python_template": "class Solution(object):\n def minimumVisitedCells(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n "31}