Skip to content

Commit 0eeefe9

Browse files
committed
286. Walls and Gates
1 parent 99b9d0a commit 0eeefe9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
607607
|46| [364_Nested_List_Weight_Sum_II](https://tinyurl.com/w5d2ff4) | [Python](https://tinyurl.com/wu6rdaw/364_Nested_List_Weight_Sum_II.py), [Swift](https://tinyurl.com/wuja3c4/364_Nested_List_Weight_Sum_II.swift) | --- | Medium |📌 Hidden DFS |
608608
|47| [1245. Tree Diameter](https://tinyurl.com/yxq4uykv) | [Python](https://tinyurl.com/wu6rdaw/1245_Tree_Diameter.py), [Swift](https://tinyurl.com/wuja3c4/1245_Tree_Diameter.swift) | [Art 1](https://www.geeksforgeeks.org/longest-path-undirected-tree/) | Medium |📌 Learned, 2 BFS. Important and interesting |
609609
|48| [1376. Time Needed to Inform All Employees](https://tinyurl.com/y3e5bb22) | [Python](https://tinyurl.com/wu6rdaw/1376_Time_Needed_to_Inform_All_Employees.py), [Swift](https://tinyurl.com/wuja3c4/1376_Time_Needed_to_Inform_All_Employees.swift) | [Art 1](https://tinyurl.com/y2lw6s4j) | Medium |📌 BFS, DFS |
610+
|49| [286. Walls and Gates](https://tinyurl.com/y3ollzsl) | [Python](https://tinyurl.com/wu6rdaw/286_Walls_and_Gates.py), [Swift](https://tinyurl.com/wuja3c4/286_Walls_and_Gates.swift) | --- | Medium |📌 BFS, DFS |
610611

611612

612613
</p>
@@ -1135,6 +1136,8 @@ Check this **[Golden](https://tinyurl.com/uboo399)** posts first.
11351136
15. [Amazon ও Google এ চাকরির সুযোগ পাওয়ার প্রস্তুতি পর্ব](https://tinyurl.com/t7dnzs8)
11361137
16. [Coding Interviews](https://tinyurl.com/rofaykt)
11371138
17. **[Tech Interview Handbook](https://tinyurl.com/rofaykt)**
1139+
18. [Complete Introduction to the 30 Most Essential Data Structures & Algorithms](https://tinyurl.com/y6e8klgj)
1140+
19. [How to Solve Any Code Challenge or Algorithm](https://tinyurl.com/y3staja5)
11381141

11391142
</p>
11401143
</details>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// DFS
2+
import Foundation
3+
class Solution {
4+
func wallsAndGates(_ rooms: inout [[Int]]) {
5+
6+
var visited = Set<String>()
7+
for i in 0..<rooms.count {
8+
for j in 0..<rooms[0].count {
9+
if rooms[i][j] == 0 {
10+
wallsAndGatesDFSHelper(&rooms, &visited, (i, j))
11+
}
12+
}
13+
}
14+
}
15+
16+
func wallsAndGatesDFSHelper(_ rooms: inout [[Int]], _ visited: inout Set<String>, _ startCoord: (Int, Int)) {
17+
for neighbor in [ (-1,0), (0,1), (1,0), (0,-1) ] {
18+
let (newX, newY) = (startCoord.0 + neighbor.0, startCoord.1 + neighbor.1)
19+
if newX >= 0 && newX < rooms.count && newY >= 0 && newY < rooms[0].count && rooms[newX][newY] != -1 && rooms[newX][newY] != 0 && !visited.contains("\(newX)-\(newY)") {
20+
if rooms[newX][newY] > rooms[startCoord.0][startCoord.1] {
21+
rooms[newX][newY] = rooms[startCoord.0][startCoord.1] + 1
22+
visited.insert("\(newX)-\(newY)")
23+
wallsAndGatesDFSHelper(&rooms, &visited, (newX, newY))
24+
visited.remove("\(newX)-\(newY)")
25+
}
26+
}
27+
}
28+
}
29+
}
30+
31+
// BFS
32+
import Foundation
33+
class Solution {
34+
func wallsAndGates(_ rooms: inout [[Int]]) {
35+
36+
var queue = [(Int, Int, Int)]()
37+
var visited = Set<String>()
38+
for i in 0..<rooms.count {
39+
for j in 0..<rooms[0].count {
40+
if rooms[i][j] == 0 {
41+
queue.append((i, j, 0))
42+
}
43+
}
44+
}
45+
46+
while !queue.isEmpty {
47+
var currentLevelLength = queue.count
48+
for _ in 1...currentLevelLength {
49+
let (x, y, val) = queue.removeFirst()
50+
51+
for neighbor in [ (-1,0), (0,1), (1,0), (0,-1) ] {
52+
let (newX, newY) = (x + neighbor.0, y + neighbor.1)
53+
if newX >= 0 && newX < rooms.count && newY >= 0 && newY < rooms[0].count && rooms[newX][newY] != -1 && rooms[newX][newY] != 0 && !visited.contains("\(newX)-\(newY)") {
54+
rooms[newX][newY] = val + 1
55+
visited.insert("\(newX)-\(newY)")
56+
queue.append((newX, newY, val + 1))
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)