-
Notifications
You must be signed in to change notification settings - Fork 110
/
200-number-of-islands.cpp
88 lines (84 loc) · 3.03 KB
/
200-number-of-islands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Iterative BFS based solution
// Time: O(mn) Space O(mn)
// However, it runs very slow
#include <queue>
class Solution {
public:
int exploreIsland(int i, int j, vector<vector<char>>& grid, vector<vector<bool>>& visited) {
int result = 0;
int rows = grid.size();
int cols = 0;
if (rows > 0) cols = grid[0].size();
queue<vector<int>> q;
q.push({i, j});
while (!q.empty()) {
vector<int> front = q.front();
q.pop();
i = front[0];
j = front[1];
if (visited[i][j]) continue;
visited[i][j] = true;
if (grid[i][j] == '1') {
result = 1;
if ((i-1 >= 0) && !visited[i-1][j]) q.push({i-1, j});
if ((i+1 < rows) && !visited[i+1][j]) q.push({i+1, j});
if ((j-1 >= 0) && !visited[i][j-1]) q.push({i, j-1});
if ((j+1 < cols) && !visited[i][j+1]) q.push({i, j+1});
}
}
return result;
}
int numIslands(vector<vector<char>>& grid) {
int result = 0;
int rows = grid.size();
int cols = 0;
if (rows > 0) cols = grid[0].size();
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (visited[i][j]) continue;
if (grid[i][j] == '1') {
result += exploreIsland(i, j, grid, visited);
} else {
visited[i][j] = true;
}
}
}
return result;
}
};
// Recursive DFS Solution
// Time: O(mn) Space O(mn)
// This runs much faster
#include <queue>
class Solution {
public:
int exploreIsland(int i, int j, int rows, int cols, vector<vector<char>>& grid, vector<vector<bool>>& visited) {
visited[i][j] = true;
if (grid[i][j] == '1') {
if ((i-1 >= 0) && !visited[i-1][j]) exploreIsland(i-1, j, rows, cols, grid, visited);
if ((i+1 < rows) && !visited[i+1][j]) exploreIsland(i+1, j, rows, cols, grid, visited);
if ((j-1 >= 0) && !visited[i][j-1]) exploreIsland(i, j-1, rows, cols, grid, visited);
if ((j+1 < cols) && !visited[i][j+1]) exploreIsland(i, j+1, rows, cols, grid, visited);
}
return 1;
}
int numIslands(vector<vector<char>>& grid) {
int result = 0;
int rows = grid.size();
int cols = 0;
if (rows > 0) cols = grid[0].size();
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (visited[i][j]) continue;
if (grid[i][j] == '1') {
result += exploreIsland(i, j, rows, cols, grid, visited);
} else {
visited[i][j] = true;
}
}
}
return result;
}
};