Skip to content

Commit 449195d

Browse files
author
Joseph Luce
authored
Update shortest_distance_from_all_buildings.md
1 parent f0a0f36 commit 449195d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

real_interview_questions/Google/shortest_distance_from_all_buildings.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
# QUESTION
2+
You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
3+
4+
Each 0 marks an empty land which you can pass by freely.
5+
Each 1 marks a building which you cannot pass through.
6+
Each 2 marks an obstacle which you cannot pass through.
7+
Example:
8+
```
9+
Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]
10+
11+
1 - 0 - 2 - 0 - 1
12+
| | | | |
13+
0 - 0 - 0 - 0 - 0
14+
| | | | |
15+
0 - 0 - 1 - 0 - 0
16+
17+
Output: 7
18+
19+
Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2),
20+
the point (1,2) is an ideal empty land to build a house, as the total
21+
travel distance of 3+3+1=7 is minimal. So return 7.
22+
Note:
23+
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
24+
25+
```
26+
127
# SOLUTION (Time Limit Exceeded)
228
```
329
class Solution(object):

0 commit comments

Comments
 (0)