|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-path-score-in-a-grid">3986. Maximum Path Score in a Grid</a></h2><h3>Medium</h3><hr><p>You are given an <code>m x n</code> grid where each cell contains one of the values 0, 1, or 2. You are also given an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>You start from the top-left corner <code>(0, 0)</code> and want to reach the bottom-right corner <code>(m - 1, n - 1)</code> by moving only <strong>right</strong> or <strong>down</strong>.</p> |
| 4 | + |
| 5 | +<p>Each cell contributes a specific score and incurs an associated cost, according to their cell values:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>0: adds 0 to your score and costs 0.</li> |
| 9 | + <li>1: adds 1 to your score and costs 1.</li> |
| 10 | + <li>2: adds 2 to your score and costs 1. </li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p>Return the <strong>maximum</strong> score achievable without exceeding a total cost of <code>k</code>, or -1 if no valid path exists.</p> |
| 14 | + |
| 15 | +<p><strong>Note:</strong> If you reach the last cell but the total cost exceeds <code>k</code>, the path is invalid.</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong class="example">Example 1:</strong></p> |
| 19 | + |
| 20 | +<div class="example-block"> |
| 21 | +<p><strong>Input:</strong> <span class="example-io">grid = [[0, 1],[2, 0]], k = 1</span></p> |
| 22 | + |
| 23 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 24 | + |
| 25 | +<p><strong>Explanation:</strong></p> |
| 26 | + |
| 27 | +<p>The optimal path is:</p> |
| 28 | + |
| 29 | +<table style="border: 1px solid black;"> |
| 30 | + <thead> |
| 31 | + <tr> |
| 32 | + <th style="border: 1px solid black;">Cell</th> |
| 33 | + <th style="border: 1px solid black;">grid[i][j]</th> |
| 34 | + <th style="border: 1px solid black;">Score</th> |
| 35 | + <th style="border: 1px solid black;">Total<br /> |
| 36 | + Score</th> |
| 37 | + <th style="border: 1px solid black;">Cost</th> |
| 38 | + <th style="border: 1px solid black;">Total<br /> |
| 39 | + Cost</th> |
| 40 | + </tr> |
| 41 | + </thead> |
| 42 | + <tbody> |
| 43 | + <tr> |
| 44 | + <td style="border: 1px solid black;">(0, 0)</td> |
| 45 | + <td style="border: 1px solid black;">0</td> |
| 46 | + <td style="border: 1px solid black;">0</td> |
| 47 | + <td style="border: 1px solid black;">0</td> |
| 48 | + <td style="border: 1px solid black;">0</td> |
| 49 | + <td style="border: 1px solid black;">0</td> |
| 50 | + </tr> |
| 51 | + <tr> |
| 52 | + <td style="border: 1px solid black;">(1, 0)</td> |
| 53 | + <td style="border: 1px solid black;">2</td> |
| 54 | + <td style="border: 1px solid black;">2</td> |
| 55 | + <td style="border: 1px solid black;">2</td> |
| 56 | + <td style="border: 1px solid black;">1</td> |
| 57 | + <td style="border: 1px solid black;">1</td> |
| 58 | + </tr> |
| 59 | + <tr> |
| 60 | + <td style="border: 1px solid black;">(1, 1)</td> |
| 61 | + <td style="border: 1px solid black;">0</td> |
| 62 | + <td style="border: 1px solid black;">0</td> |
| 63 | + <td style="border: 1px solid black;">2</td> |
| 64 | + <td style="border: 1px solid black;">0</td> |
| 65 | + <td style="border: 1px solid black;">1</td> |
| 66 | + </tr> |
| 67 | + </tbody> |
| 68 | +</table> |
| 69 | + |
| 70 | +<p>Thus, the maximum possible score is 2.</p> |
| 71 | +</div> |
| 72 | + |
| 73 | +<p><strong class="example">Example 2:</strong></p> |
| 74 | + |
| 75 | +<div class="example-block"> |
| 76 | +<p><strong>Input:</strong> <span class="example-io">grid = [[0, 1],[1, 2]], k = 1</span></p> |
| 77 | + |
| 78 | +<p><strong>Output:</strong> <span class="example-io">-1</span></p> |
| 79 | + |
| 80 | +<p><strong>Explanation:</strong></p> |
| 81 | + |
| 82 | +<p>There is no path that reaches cell <code>(1, 1)</code> without exceeding cost k. Thus, the answer is -1.</p> |
| 83 | +</div> |
| 84 | + |
| 85 | +<p> </p> |
| 86 | +<p><strong>Constraints:</strong></p> |
| 87 | + |
| 88 | +<ul> |
| 89 | + <li><code>1 <= m, n <= 200</code></li> |
| 90 | + <li><code>0 <= k <= 10<sup>3</sup></code></li> |
| 91 | + <li><code><sup></sup>grid[0][0] == 0</code></li> |
| 92 | + <li><code>0 <= grid[i][j] <= 2</code></li> |
| 93 | +</ul> |
0 commit comments