Skip to content

Commit e7e8311

Browse files
committed
Sync LeetCode submission Runtime - 1140 ms (5.30%), Memory - 18.8 MB (80.37%)
1 parent 55b62e3 commit e7e8311

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<p>Given an <code>m x n</code> grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of <code>grid[i][j]</code> can be:</p>
2+
3+
<ul>
4+
<li><code>1</code> which means go to the cell to the right. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j + 1]</code>)</li>
5+
<li><code>2</code> which means go to the cell to the left. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j - 1]</code>)</li>
6+
<li><code>3</code> which means go to the lower cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i + 1][j]</code>)</li>
7+
<li><code>4</code> which means go to the upper cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i - 1][j]</code>)</li>
8+
</ul>
9+
10+
<p>Notice that there could be some signs on the cells of the grid that point outside the grid.</p>
11+
12+
<p>You will initially start at the upper left cell <code>(0, 0)</code>. A valid path in the grid is a path that starts from the upper left cell <code>(0, 0)</code> and ends at the bottom-right cell <code>(m - 1, n - 1)</code> following the signs on the grid. The valid path does not have to be the shortest.</p>
13+
14+
<p>You can modify the sign on a cell with <code>cost = 1</code>. You can modify the sign on a cell <strong>one time only</strong>.</p>
15+
16+
<p>Return <em>the minimum cost to make the grid have at least one valid path</em>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid1.png" style="width: 400px; height: 390px;" />
21+
<pre>
22+
<strong>Input:</strong> grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
23+
<strong>Output:</strong> 3
24+
<strong>Explanation:</strong> You will start at point (0, 0).
25+
The path to (3, 3) is as follows. (0, 0) --&gt; (0, 1) --&gt; (0, 2) --&gt; (0, 3) change the arrow to down with cost = 1 --&gt; (1, 3) --&gt; (1, 2) --&gt; (1, 1) --&gt; (1, 0) change the arrow to down with cost = 1 --&gt; (2, 0) --&gt; (2, 1) --&gt; (2, 2) --&gt; (2, 3) change the arrow to down with cost = 1 --&gt; (3, 3)
26+
The total cost = 3.
27+
</pre>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid2.png" style="width: 350px; height: 341px;" />
31+
<pre>
32+
<strong>Input:</strong> grid = [[1,1,3],[3,2,2],[1,1,4]]
33+
<strong>Output:</strong> 0
34+
<strong>Explanation:</strong> You can follow the path from (0, 0) to (2, 2).
35+
</pre>
36+
37+
<p><strong class="example">Example 3:</strong></p>
38+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid3.png" style="width: 200px; height: 192px;" />
39+
<pre>
40+
<strong>Input:</strong> grid = [[1,2],[4,3]]
41+
<strong>Output:</strong> 1
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>m == grid.length</code></li>
49+
<li><code>n == grid[i].length</code></li>
50+
<li><code>1 &lt;= m, n &lt;= 100</code></li>
51+
<li><code>1 &lt;= grid[i][j] &lt;= 4</code></li>
52+
</ul>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Approach 1: Dynamic Programming
2+
3+
# n = no. of row, m = no. of cols
4+
# Time: O((n * m) ^ 2)
5+
# Space: O(n * m)
6+
7+
class Solution:
8+
def minCost(self, grid: List[List[int]]) -> int:
9+
num_rows, num_cols = len(grid), len(grid[0])
10+
11+
min_changes = [[float('inf')] * num_cols for _ in range(num_rows)]
12+
min_changes[0][0] = 0
13+
14+
while True:
15+
# Store previous state to check for convergence
16+
prev_state = [row[:] for row in min_changes]
17+
18+
# Forward pass: check cells coming from left and top
19+
for row in range(num_rows):
20+
for col in range(num_cols):
21+
# Check cell above
22+
if row > 0:
23+
min_changes[row][col] = min(min_changes[row][col],
24+
min_changes[row - 1][col] + (0 if grid[row - 1][col] == 3 else 1))
25+
26+
# Check cell to the left
27+
if col > 0:
28+
min_changes[row][col] = min(min_changes[row][col],
29+
min_changes[row][col - 1] + (0 if grid[row][col - 1] == 1 else 1))
30+
31+
# Backward Pass: check cells coming from right and bottom
32+
for row in range(num_rows - 1, -1, -1):
33+
for col in range(num_cols - 1, -1, -1):
34+
# Check cell below
35+
if row < num_rows - 1:
36+
min_changes[row][col] = min(min_changes[row][col],
37+
min_changes[row + 1][col] + (0 if grid[row + 1][col] == 4 else 1))
38+
39+
# Check cell to the right
40+
if col < num_cols - 1:
41+
min_changes[row][col] = min(min_changes[row][col],
42+
min_changes[row][col + 1] + (0 if grid[row][col + 1] == 2 else 1))
43+
44+
# If no changes were made in this iteration, we've found optimal solution
45+
if min_changes == prev_state:
46+
break
47+
48+
return min_changes[num_rows - 1][num_cols - 1]
49+

0 commit comments

Comments
 (0)