Skip to content

Commit 828aa06

Browse files
committed
Sync LeetCode submission Runtime - 2017 ms (69.76%), Memory - 18.1 MB (87.55%)
1 parent 0f73308 commit 828aa06

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>You are given an <code>m x n</code> grid <code>grid</code> of values <code>0</code>, <code>1</code>, or <code>2</code>, where:</p>
2+
3+
<ul>
4+
<li>each <code>0</code> marks <strong>an empty land</strong> that you can pass by freely,</li>
5+
<li>each <code>1</code> marks <strong>a building</strong> that you cannot pass through, and</li>
6+
<li>each <code>2</code> marks <strong>an obstacle</strong> that you cannot pass through.</li>
7+
</ul>
8+
9+
<p>You want to build a house on an empty land that reaches all buildings in the <strong>shortest total travel</strong> distance. You can only move up, down, left, and right.</p>
10+
11+
<p>Return <em>the <strong>shortest travel distance</strong> for such a house</em>. If it is not possible to build such a house according to the above rules, return <code>-1</code>.</p>
12+
13+
<p>The <strong>total travel distance</strong> is the sum of the distances between the houses of the friends and the meeting point.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/buildings-grid.jpg" style="width: 413px; height: 253px;" />
18+
<pre>
19+
<strong>Input:</strong> grid = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]
20+
<strong>Output:</strong> 7
21+
<strong>Explanation:</strong> Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2).
22+
The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal.
23+
So return 7.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> grid = [[1,0]]
30+
<strong>Output:</strong> 1
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> grid = [[1]]
37+
<strong>Output:</strong> -1
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>m == grid.length</code></li>
45+
<li><code>n == grid[i].length</code></li>
46+
<li><code>1 &lt;= m, n &lt;= 50</code></li>
47+
<li><code>grid[i][j]</code> is either <code>0</code>, <code>1</code>, or <code>2</code>.</li>
48+
<li>There will be <strong>at least one</strong> building in the <code>grid</code>.</li>
49+
</ul>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Approach 3: BFS from Houses to Empty Land (Optimized)
2+
3+
from collections import deque
4+
5+
class Solution:
6+
def shortestDistance(self, grid: List[List[int]]) -> int:
7+
rows, cols = len(grid), len(grid[0])
8+
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
9+
10+
total_sum = [[0] * cols for _ in range(rows)]
11+
12+
def bfs(row, col, curr_count):
13+
min_dist = float('inf')
14+
queue = deque()
15+
queue.append([row, col, 0])
16+
17+
while queue:
18+
curr_row, curr_col, curr_step = queue.popleft()
19+
for d in dirs:
20+
next_row, next_col = curr_row + d[0], curr_col + d[1]
21+
22+
if (0 <= next_row < rows and
23+
0 <= next_col < cols and
24+
grid[next_row][next_col] == -curr_count):
25+
total_sum[next_row][next_col] += curr_step + 1
26+
min_dist = min(min_dist, total_sum[next_row][next_col])
27+
grid[next_row][next_col] -= 1
28+
queue.append([next_row, next_col, curr_step + 1])
29+
30+
return min_dist
31+
32+
count = 0
33+
for row in range(rows):
34+
for col in range(cols):
35+
if grid[row][col] == 1:
36+
min_dist = bfs(row, col, count)
37+
count += 1
38+
if min_dist == float('inf'):
39+
return -1
40+
41+
return min_dist

0 commit comments

Comments
 (0)