|
| 1 | +<h2><a href="https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/">1293. Shortest Path in a Grid with Obstacles Elimination</a></h2><h3>Hard</h3><hr><div><p>You are given an <code>m x n</code> integer matrix <code>grid</code> where each cell is either <code>0</code> (empty) or <code>1</code> (obstacle). You can move up, down, left, or right from and to an empty cell in <strong>one step</strong>.</p> |
| 2 | + |
| 3 | +<p>Return <em>the minimum number of <strong>steps</strong> to walk from the upper left corner </em><code>(0, 0)</code><em> to the lower right corner </em><code>(m - 1, n - 1)</code><em> given that you can eliminate <strong>at most</strong> </em><code>k</code><em> obstacles</em>. If it is not possible to find such walk return <code>-1</code>.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/09/30/short1-grid.jpg" style="width: 244px; height: 405px;"> |
| 8 | +<pre><strong>Input:</strong> grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1 |
| 9 | +<strong>Output:</strong> 6 |
| 10 | +<strong>Explanation:</strong> |
| 11 | +The shortest path without eliminating any obstacle is 10. |
| 12 | +The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> <strong>(3,2)</strong> -> (4,2). |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong class="example">Example 2:</strong></p> |
| 16 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/09/30/short2-grid.jpg" style="width: 244px; height: 245px;"> |
| 17 | +<pre><strong>Input:</strong> grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1 |
| 18 | +<strong>Output:</strong> -1 |
| 19 | +<strong>Explanation:</strong> We need to eliminate at least two obstacles to find such a walk. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong>Constraints:</strong></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li><code>m == grid.length</code></li> |
| 27 | + <li><code>n == grid[i].length</code></li> |
| 28 | + <li><code>1 <= m, n <= 40</code></li> |
| 29 | + <li><code>1 <= k <= m * n</code></li> |
| 30 | + <li><code>grid[i][j]</code> is either <code>0</code> <strong>or</strong> <code>1</code>.</li> |
| 31 | + <li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li> |
| 32 | +</ul> |
| 33 | +</div> |
0 commit comments