Skip to content

Commit e94b40b

Browse files
committed
1254_Number_of_Closed_Islands
1 parent 61ca2b9 commit e94b40b

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,9 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
615615
|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 |
616616
|50| [1138. Alphabet Board Path](https://tinyurl.com/y2xk6r98) | [Python](https://tinyurl.com/wu6rdaw/1138_Alphabet_Board_Path.py), [Swift](https://tinyurl.com/wuja3c4/1138_Alphabet_Board_Path.swift) | --- | Medium |📌 BFS |
617617
|51| [690. Employee Importance](https://tinyurl.com/yyweuk43) | [Python](https://tinyurl.com/wu6rdaw/690_Employee_Importance.py), [Swift](https://tinyurl.com/wuja3c4/690_Employee_Importance.swift) | --- | Easy |📌 BFS |
618-
|52| **[1631. Path With Minimum Effort](https://tinyurl.com/y3nh25d9) **| [Python](https://tinyurl.com/wu6rdaw/1631_Path_With_Minimum_Effort.py), [Swift](https://tinyurl.com/wuja3c4/1631_Path_With_Minimum_Effort.swift) | --- | Medium |📌 BFS, DFS, Binary search |
619-
|53| **[547. Friend Circles](https://tinyurl.com/r39pov7) **| [Python](https://tinyurl.com/wu6rdaw/547_Friend_Circles.py), [Swift](https://tinyurl.com/wuja3c4/547_Friend_Circles.swift) | [Art 1](https://tinyurl.com/y6qm62wq), [Art 2](https://tinyurl.com/y2gk7dq4) | Medium |📌 BFS, DFS, Union Find |
618+
|52| **[1631. Path With Minimum Effort](https://tinyurl.com/y3nh25d9)** | [Python](https://tinyurl.com/wu6rdaw/1631_Path_With_Minimum_Effort.py), [Swift](https://tinyurl.com/wuja3c4/1631_Path_With_Minimum_Effort.swift) | --- | Medium |📌 BFS, DFS, Binary search |
619+
|53| **[547. Friend Circles](https://tinyurl.com/r39pov7)** | [Python](https://tinyurl.com/wu6rdaw/547_Friend_Circles.py), [Swift](https://tinyurl.com/wuja3c4/547_Friend_Circles.swift) | [Art 1](https://tinyurl.com/y6qm62wq), [Art 2](https://tinyurl.com/y2gk7dq4) | Medium |📌 BFS, DFS, Union Find |
620+
|53| **[1254. Number of Closed Islands](https://tinyurl.com/r39pov7)** | [Swift](https://tinyurl.com/wuja3c4/1254_Number_of_Closed_Islands.swift) | [Art 1](https://tinyurl.com/y6qm62wq), [Art 2](https://tinyurl.com/y2gk7dq4) | Medium |📌 BFS, DFS, Union Find |
620621

621622

622623
</p>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// DFS. Accepted. Time and Space: O(mn)
2+
class Solution {
3+
func closedIsland(_ grid: [[Int]]) -> Int {
4+
guard grid.count > 2,
5+
grid[0].count > 2 else {
6+
return 0
7+
}
8+
var (grid, ilandCount) = (grid, 0)
9+
for row in 1..<grid.count - 1 {
10+
for col in 1..<grid[0].count - 1 {
11+
let value = grid[row][col]
12+
if value == 0 {
13+
grid[row][col] = 2
14+
ilandCount += dfs(&grid, row, col)
15+
}
16+
}
17+
}
18+
return ilandCount
19+
}
20+
21+
@discardableResult
22+
func dfs(_ grid: inout [[Int]], _ row: Int, _ col: Int) -> Int {
23+
var results = [Int]()
24+
for (newRow, newCol) in [(row - 1, col), (row, col + 1), (row + 1, col), (row, col - 1)] {
25+
if newRow >= 0 && newRow < grid.count && newCol >= 0 && newCol < grid[0].count {
26+
if newRow == 0 || newRow == grid.count - 1 || newCol == 0 || newCol == grid[0].count - 1 {
27+
if grid[newRow][newCol] == 0 {
28+
results.append(0)
29+
grid[newRow][newCol] = 2
30+
dfs(&grid, newRow, newCol)
31+
}
32+
} else {
33+
if grid[newRow][newCol] == 0 {
34+
grid[newRow][newCol] = 2
35+
results.append(dfs(&grid, newRow, newCol))
36+
}
37+
}
38+
}
39+
}
40+
return results.allSatisfy { $0 == 1 } ? 1 : 0
41+
}
42+
}
43+
44+
// BFS. Accepted. Time and Space: O(mn)
45+
class Solution {
46+
func closedIsland(_ grid: [[Int]]) -> Int {
47+
guard grid.count > 2,
48+
grid[0].count > 2 else {
49+
return 0
50+
}
51+
var (grid, ilandCount) = (grid, 0)
52+
for row in 1..<grid.count - 1 {
53+
for col in 1..<grid[0].count - 1 {
54+
let value = grid[row][col]
55+
if value == 0 {
56+
grid[row][col] = 2
57+
ilandCount += bfs(&grid, row, col)
58+
}
59+
}
60+
}
61+
return ilandCount
62+
}
63+
64+
@discardableResult
65+
func bfs(_ grid: inout [[Int]], _ row: Int, _ col: Int) -> Int {
66+
var isClosed = true
67+
var queue = [(Int, Int)]() // (row, col) of cells with value of 0
68+
queue.append((row, col))
69+
while !queue.isEmpty {
70+
let (currentRow, currentCol) = queue.removeFirst()
71+
for (newRow, newCol) in [(currentRow - 1, currentCol), (currentRow, currentCol + 1), (currentRow + 1, currentCol), (currentRow, currentCol - 1)] {
72+
if newRow >= 0 && newRow < grid.count && newCol >= 0 && newCol < grid[0].count {
73+
if newRow == 0 || newRow == grid.count - 1 || newCol == 0 || newCol == grid[0].count - 1 {
74+
if grid[newRow][newCol] == 0 {
75+
isClosed = false
76+
grid[newRow][newCol] = 2
77+
queue.append((newRow, newCol))
78+
}
79+
} else {
80+
if grid[newRow][newCol] == 0 {
81+
grid[newRow][newCol] = 2
82+
queue.append((newRow, newCol))
83+
}
84+
}
85+
}
86+
}
87+
}
88+
return isClosed ? 1 : 0
89+
}
90+
}

0 commit comments

Comments
 (0)