Skip to content

Commit 318c233

Browse files
committed
Added Solution for 300-400q/317 300-400q/351
1 parent 86fc9b7 commit 318c233

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

300-400q/317.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'''
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+
For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2):
8+
9+
1 - 0 - 2 - 0 - 1
10+
| | | | |
11+
0 - 0 - 0 - 0 - 0
12+
| | | | |
13+
0 - 0 - 1 - 0 - 0
14+
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. So return 7.
15+
16+
Note:
17+
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
18+
'''
19+
20+
21+
class Solution(object):
22+
def shortestDistance(self, grid):
23+
if not grid:
24+
return -1
25+
26+
def bfs(grid, distance_reach_map, row, col):
27+
if(row < 0 or row > len(grid) or col < 0 or col > len(grid[0])):
28+
return
29+
queue = [[row, col]]
30+
qdist = [1]
31+
32+
direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
33+
while queue:
34+
x, y = queue.pop(0)
35+
curr_dist = qdist.pop(0)
36+
37+
for dx, dy in direction:
38+
new_x, new_y = x+dx, y+dy
39+
if((0 <= new_x < len(grid)) and (0 <= new_y < len(grid[0])) and grid[new_x][new_y] == 0):
40+
grid[new_x][new_y] = -1
41+
queue.append([new_x, new_y])
42+
43+
44+
temp = distance_reach_map[new_x][new_y]
45+
dist, reach = temp[0], temp[1]
46+
dist += curr_dist
47+
reach += 1
48+
distance_reach_map[new_x][new_y] = [dist, reach]
49+
qdist.append(curr_dist+1)
50+
51+
for row in range(len(grid)):
52+
for col in range(len(grid[0])):
53+
if grid[row][col] == -1:
54+
grid[row][col] =0
55+
56+
r_len, c_len = len(grid), len(grid[0])
57+
distance_reach_map = [[[0, 0]]*c_len for _ in range(r_len)]
58+
buildings = 0
59+
for row in range(len(grid)):
60+
for col in range(len(grid[0])):
61+
if grid[row][col] == 1:
62+
bfs(grid, distance_reach_map, row, col)
63+
buildings += 1
64+
65+
result = float('inf')
66+
for row in range(r_len):
67+
for col in range(c_len):
68+
dist, reach = distance_reach_map[row][col]
69+
if reach == buildings:
70+
result = min(result, dist)
71+
return result
72+
73+
solution = Solution()
74+
grid = [[1, 0, 2, 0, 1],
75+
[0, 0, 0, 0, 0],
76+
[0, 0, 1, 0 ,0]]
77+
print solution.shortestDistance(grid)
78+
79+
80+
81+
82+

300-400q/351.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.
3+
4+
Rules for a valid pattern:
5+
Each pattern must connect at least m keys and at most n keys.
6+
All the keys must be distinct.
7+
If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
8+
The order of keys used matters.
9+
10+
Explanation:
11+
| 1 | 2 | 3 |
12+
| 4 | 5 | 6 |
13+
| 7 | 8 | 9 |
14+
Invalid move: 4 - 1 - 3 - 6
15+
Line 1 - 3 passes through key 2 which had not been selected in the pattern.
16+
17+
Invalid move: 4 - 1 - 9 - 2
18+
Line 1 - 9 passes through key 5 which had not been selected in the pattern.
19+
20+
Valid move: 2 - 4 - 1 - 3 - 6
21+
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern
22+
23+
Valid move: 6 - 5 - 4 - 1 - 9 - 2
24+
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.
25+
26+
Example:
27+
Given m = 1, n = 1, return 9.
28+
'''
29+
30+
class Solution(object):
31+
def numberOfPatterns(self, m, n):
32+
def dfs(reamin, current, visited, skip):
33+
if reamin < 0:
34+
return 0
35+
if reamin == 0:
36+
return 1
37+
visited[current] = True
38+
result = 0
39+
for index in range(1, 10):
40+
if not visited[index] and (skip[current][index] == 0 || visited[skip[current][index]])
41+
result += dfs(reamin-1, index, visited, skip)
42+
visited[current] = False
43+
return result
44+
45+
skip = [[0 for _ in range(10)] for _ in range(10)]
46+
skip[1][3] = skip[3][1] = 2
47+
skip[1][7] = skip[7][1] = 4
48+
skip[3][9] = skip[9][3] = 6
49+
skip[7][9] = skip[9][7] = 8
50+
skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5
51+
visited = [False for _ in range(10)]
52+
53+
result = 0
54+
for index in range(m, n):
55+
result += dfs(index-1, 1, visited, skip)
56+
result += dfs(index-1, 2, visited, skip)
57+
result += dfs(index-1, 5, visited, skip)
58+
return result

0 commit comments

Comments
 (0)