Leetcode: Walls and Gates
来源:互联网 发布:qq旋风mac 编辑:程序博客网 时间:2024/05/22 06:38
Question
You are given a m x n 2D grid initialized with these three possible values.
-1 - A wall or an obstacle.
0 - A gate.
INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
For example, given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
Hide Company Tags Facebook
Hide Tags Breadth-first Search
Hide Similar Problems (M) Surrounded Regions (M) Number of Islands (H) Shortest Distance from All Buildings
My first try
if there are k zero elements,
size(rooms) = (m,n)
Time complexity: O( kmn )
class Solution(object): def wallsAndGates(self, rooms): """ :type rooms: List[List[int]] :rtype: void Do not return anything, modify rooms in-place instead. """ if len(rooms)==0 or len(rooms[0])==0: return zero_list = [] for i in range(len(rooms)): for j in range(len(rooms[0])): if rooms[i][j]==0: zero_list.append( (i,j) ) for zero_item in zero_list: self.traverse4zero( zero_item, rooms ) def traverse4zero(self, zero_item, rooms): #print "abc" bfs_list = [zero_item] dict_direct = [(-1,0), (1,0), (0,1), (0,-1)] visited = [ [0]*len(rooms[0]) for dummy in range(len(rooms)) ] visited[zero_item[0]][zero_item[1]] = 1 while bfs_list!=[]: #print bfs_list cur_item = bfs_list[0] bfs_list = bfs_list[1:] self.eachdirection( rooms, cur_item, visited, bfs_list, dict_direct ) return def eachdirection(self, rooms, cur_item, visited, bfs_list, dict_direct): for i in range(4): next_i = cur_item[0] + dict_direct[i][0] next_j = cur_item[1] + dict_direct[i][1] if next_i<0 or next_i>=len(rooms) \ or next_j<0 or next_j>=len(rooms[0]) \ or rooms[next_i][next_j]==0 \ or rooms[next_i][next_j]==-1 \ or visited[next_i][next_j]==1 \ or rooms[cur_item[0]][cur_item[1]]+1 >= rooms[next_i][next_j]: continue rooms[next_i][next_j] = min( rooms[next_i][next_j], \ rooms[cur_item[0]][cur_item[1]]+1 ) bfs_list.append( (next_i, next_j) ) visited[next_i][next_j] = 1 return
Solution
Accept
Time: 540ms
class Solution(object): def wallsAndGates(self, rooms): """ :type rooms: List[List[int]] :rtype: void Do not return anything, modify rooms in-place instead. """ if len(rooms)==0 or len(rooms[0])==0: return zero_list = [] for i in range(len(rooms)): for j in range(len(rooms[0])): if rooms[i][j]==0: self.traverse4zero( (i,j), rooms ) def traverse4zero(self, zero_item, rooms): bfs_list = [zero_item] dict_direct = [(-1,0), (1,0), (0,1), (0,-1)] while bfs_list!=[]: cur_item = bfs_list[0] bfs_list = bfs_list[1:] for i in range(4): next_i = cur_item[0] + dict_direct[i][0] next_j = cur_item[1] + dict_direct[i][1] if next_i<0 or next_i>=len(rooms) \ or next_j<0 or next_j>=len(rooms[0]) \ or rooms[next_i][next_j]<=0 \ or rooms[cur_item[0]][cur_item[1]]+1 >= rooms[next_i][next_j]: continue rooms[next_i][next_j] = min( rooms[next_i][next_j], \ rooms[cur_item[0]][cur_item[1]]+1 ) bfs_list.append( (next_i, next_j) ) return
Solution2
Accept
Time: 588ms
This solution is different from solution1. BFS is done for each zero element at the same time.
class Solution(object): def wallsAndGates(self, rooms): """ :type rooms: List[List[int]] :rtype: void Do not return anything, modify rooms in-place instead. """ if len(rooms)==0 or len(rooms[0])==0: return bfs_list = [] for i in range(len(rooms)): for j in range(len(rooms[0])): if rooms[i][j]==0: bfs_list.append( (i,j) ) dict_direct = [(-1,0), (1,0), (0,1), (0,-1)] while bfs_list!=[]: cur_item = bfs_list[0] del bfs_list[0] for i in range(4): next_i = cur_item[0] + dict_direct[i][0] next_j = cur_item[1] + dict_direct[i][1] if next_i<0 or next_i>=len(rooms) \ or next_j<0 or next_j>=len(rooms[0]) \ or rooms[cur_item[0]][cur_item[1]]+1 >= rooms[next_i][next_j]: continue rooms[next_i][next_j] = rooms[cur_item[0]][cur_item[1]]+1 bfs_list.append( (next_i, next_j) ) return
Take home message
For popping the first element of list, use
del bfs_list[0]
instead of
bfs_list = bfs_list[1:]
- *LeetCode-Walls and Gates
- Leetcode: Walls and Gates
- leetcode 286: Walls and Gates
- Leetcode 286.Walls and Gates
- [Leetcode 286]: Walls and Gates
- LeetCode 286. Walls and Gates
- Leetcode 286. Walls and Gates
- LeetCode 286. Walls and Gates
- LeetCode 题解(259) : Walls and Gates
- [leetcode] 286. Walls and Gates 解题报告
- [Leetcode] 286. Walls and Gates 解题报告
- [LeetCode286]Walls and Gates
- Walls and Gates
- Walls and Gates
- Walls and Gates
- Walls and Gates
- 286 Walls and Gates
- 286. Walls and Gates
- svn常用操作
- 对话框DiaLog
- 傅里叶变换
- python使用cx_oracle连接oracle数据库
- Android开发获取GPS位置,包含基站\wifi\gps 几种方法
- Leetcode: Walls and Gates
- Mac下usb转串口Qt开发
- bootstrap ace admin 整合java HTML5 全新高大尚web后台框架
- pg_rman 实现对postgresql数据库的备份,恢复与管理
- file转换成bytes
- 文件拷贝的实现
- [maven+javancss] Can't process temp ncss xml file.
- (java集合原理)--01 HashMap的实现原理
- **Eclipse Access Restriction**