Skip to content

Commit 712d3f8

Browse files
committed
update 200
1 parent 3ee4d9f commit 712d3f8

File tree

4 files changed

+250
-121
lines changed

4 files changed

+250
-121
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ This is an open-source project that is continually updated
1010

1111
# 统计/Statistic
1212

13-
**2023:** 178 problems
13+
**2023:** 177 problems
1414

15-
**2024:** 255 problems
15+
**2024:** 256 problems
1616

1717
**Total:** 433 problems
1818

19-
**Updated:** 2024-09-10 18:44:26
19+
**Updated:** 2024-09-11 14:53:55
2020

2121
## 链接/Links
2222

@@ -518,7 +518,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
518518

519519
## Breadth-First Search
520520

521-
| Update | Problem(24) | Solution | Tag | Time | Space | Ref |
521+
| Update | Problem(25) | Solution | Tag | Time | Space | Ref |
522522
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
523523
| 2024-06 | [Leetcode-102. Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/) | [c++](./leetcode/102.binary-tree-level-order-traversal.cpp), [python3](./leetcode/102.binary-tree-level-order-traversal.py) | Breadth-First Search | O\(N\) | O\(Width\) | - |
524524
| 2024-06 | [Leetcode-297. Serialize And Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) | [c++](./leetcode/297.serialize-and-deserialize-binary-tree.cpp), [python3](./leetcode/297.serialize-and-deserialize-binary-tree.py) | Breadth-First Search | O\(N\) | O\(N\) | - |
@@ -544,10 +544,11 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
544544
| 2024-08 | [Leetcode-684. Redundant Connection](https://leetcode.com/problems/redundant-connection/description/) | [c++](./leetcode/684.redundant-connection.cpp), [python3](./leetcode/684.redundant-connection.py) | Breadth-First Search | O\(N\) | O\(1\) | - |
545545
| 2024-08 | [Leetcode-1631. Path With Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/description/) | [c++](./leetcode/1631.path-with-minimum-effort.cpp), [python3](./leetcode/1631.path-with-minimum-effort.py) | Breadth-First Search | O\(NMlogNM\) | O\(NM\) | - |
546546
| 2024-09 | [Leetcode-111. Minimum Depth Of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) | [c++](./leetcode/111.minimum-depth-of-binary-tree.cpp), [python3](./leetcode/111.minimum-depth-of-binary-tree.py) | Breadth-First Search | O\(N\) | O\(W\) | - |
547+
| 2024-09 | [Leetcode-200. Number Of Islands](https://leetcode.com/problems/number-of-islands/description/) | [c++](./leetcode/200.number-of-islands.cpp), [python3](./leetcode/200.number-of-islands.py) | Breadth-First Search | O\(MN\) | O\(MN\) | - |
547548

548549
## Depth-First Search
549550

550-
| Update | Problem(37) | Solution | Tag | Time | Space | Ref |
551+
| Update | Problem(38) | Solution | Tag | Time | Space | Ref |
551552
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
552553
| 2024-06 | [Leetcode-297. Serialize And Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) | [c++](./leetcode/297.serialize-and-deserialize-binary-tree.cpp), [python3](./leetcode/297.serialize-and-deserialize-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | - |
553554
| 2024-07 | [Leetcode-695. Max Area Of Island](https://leetcode.com/problems/max-area-of-island/description/) | [c++](./leetcode/695.max-area-of-island.cpp), [python3](./leetcode/695.max-area-of-island.py) | Depth-First Search | O\(MN\) | O\(MN\) | - |
@@ -586,6 +587,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
586587
| 2024-08 | [Leetcode-684. Redundant Connection](https://leetcode.com/problems/redundant-connection/description/) | [c++](./leetcode/684.redundant-connection.cpp), [python3](./leetcode/684.redundant-connection.py) | Depth-First Search | O\(N\) | O\(1\) | - |
587588
| 2024-08 | [Leetcode-1631. Path With Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/description/) | [c++](./leetcode/1631.path-with-minimum-effort.cpp), [python3](./leetcode/1631.path-with-minimum-effort.py) | Depth-First Search | O\(NMlogNM\) | O\(NM\) | - |
588589
| 2024-09 | [Leetcode-111. Minimum Depth Of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) | [c++](./leetcode/111.minimum-depth-of-binary-tree.cpp), [python3](./leetcode/111.minimum-depth-of-binary-tree.py) | Depth-First Search | O\(N\) | O\(W\) | - |
590+
| 2024-09 | [Leetcode-200. Number Of Islands](https://leetcode.com/problems/number-of-islands/description/) | [c++](./leetcode/200.number-of-islands.cpp), [python3](./leetcode/200.number-of-islands.py) | Depth-First Search | O\(MN\) | O\(MN\) | - |
589591

590592
## Topological Sort
591593

@@ -676,7 +678,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
676678

677679
## Union Find
678680

679-
| Update | Problem(8) | Solution | Tag | Time | Space | Ref |
681+
| Update | Problem(9) | Solution | Tag | Time | Space | Ref |
680682
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
681683
| 2024-07 | [Leetcode-695. Max Area Of Island](https://leetcode.com/problems/max-area-of-island/description/) | [c++](./leetcode/695.max-area-of-island.cpp), [python3](./leetcode/695.max-area-of-island.py) | Union Find | O\(MN\) | O\(MN\) | - |
682684
| 2024-07 | [Leetcode-547. Number Of Provinces](https://leetcode.com/problems/number-of-provinces/description/) | [c++](./leetcode/547.number-of-provinces.cpp), [python3](./leetcode/547.number-of-provinces.py) | Union Find | O\(N^2\) | O\(N\) | - |
@@ -686,6 +688,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
686688
| 2024-08 | [Lintcode-3672. Connecting Cities With Minimum Cost](https://www.lintcode.com/problem/connecting-cities-with-minimum-cost) | [c++](./lintcode/3672.connecting-cities-with-minimum-cost.cpp), [python3](./lintcode/3672.connecting-cities-with-minimum-cost.py) | Union Find | O\(ElogE\) | O\(E\+V\) | - |
687689
| 2024-08 | [Leetcode-684. Redundant Connection](https://leetcode.com/problems/redundant-connection/description/) | [c++](./leetcode/684.redundant-connection.cpp), [python3](./leetcode/684.redundant-connection.py) | Union Find | O\(N\) | O\(1\) | - |
688690
| 2024-08 | [Leetcode-1631. Path With Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/description/) | [c++](./leetcode/1631.path-with-minimum-effort.cpp), [python3](./leetcode/1631.path-with-minimum-effort.py) | Union Find | O\(NMlogNM\) | O\(NM\) | - |
691+
| 2024-09 | [Leetcode-200. Number Of Islands](https://leetcode.com/problems/number-of-islands/description/) | [c++](./leetcode/200.number-of-islands.cpp), [python3](./leetcode/200.number-of-islands.py) | Union Find | O\(MN\) | O\(MN\) | - |
689692

690693
## Trie
691694

@@ -711,7 +714,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
711714

712715
## Other
713716

714-
| Update | Problem(182) | Solution | Tag | Time | Space | Ref |
717+
| Update | Problem(181) | Solution | Tag | Time | Space | Ref |
715718
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
716719
| 2023-09 | [Leetcode-2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/) | [c++](./leetcode/2.add-two-numbers.cpp), [python3](./leetcode/2.add-two-numbers.py) | Other | \- | \- | - |
717720
| 2023-09 | [Leetcode-7. Reverse Integer](https://leetcode.com/problems/reverse-integer/description/) | [c++](./leetcode/7.reverse-integer.cpp), [python3](./leetcode/7.reverse-integer.py) | Other | \- | \- | - |
@@ -771,7 +774,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
771774
| 2023-09 | [Leetcode-163. Missing Ranges](https://leetcode.com/problems/missing-ranges/description/) | [c++](./leetcode/163.missing-ranges.cpp), [python3](./leetcode/163.missing-ranges.py) | Other | \- | \- | - |
772775
| 2023-09 | [Leetcode-170. Two Sum Iii Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) | [c++](./leetcode/170.two-sum-iii-data-structure-design.cpp), [python3](./leetcode/170.two-sum-iii-data-structure-design.py) | Other | \- | \- | - |
773776
| 2023-09 | [Leetcode-175. Combine Two Tables](https://leetcode.com/problems/combine-two-tables/description/) | [sql](./leetcode/175.combine-two-tables.sql) | Other | \- | \- | - |
774-
| 2023-09 | [Leetcode-200. Number Of Islands](https://leetcode.com/problems/number-of-islands/description/) | [c++](./leetcode/200.number-of-islands.cpp), [python3](./leetcode/200.number-of-islands.py) | Other | \- | \- | - |
775777
| 2023-09 | [Leetcode-203. Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/description/) | [python3](./leetcode/203.remove-linked-list-elements.py) | Other | \- | \- | - |
776778
| 2023-09 | [Leetcode-207. Course Schedule](https://leetcode.com/problems/course-schedule/description/) | [c++](./leetcode/207.course-schedule.cpp), [python3](./leetcode/207.course-schedule.py) | Other | \- | \- | - |
777779
| 2023-09 | [Leetcode-209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/description/) | [c++](./leetcode/209.minimum-size-subarray-sum.cpp), [python3](./leetcode/209.minimum-size-subarray-sum.py) | Other | \- | \- | - |

leetcode/200.number-of-islands.cpp

Lines changed: 123 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,138 @@
1+
// Tag: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
2+
// Time: O(MN)
3+
// Space: O(MN)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
8+
// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: grid = [
13+
// ["1","1","1","1","0"],
14+
// ["1","1","0","1","0"],
15+
// ["1","1","0","0","0"],
16+
// ["0","0","0","0","0"]
17+
// ]
18+
// Output: 1
19+
//
20+
// Example 2:
21+
//
22+
// Input: grid = [
23+
// ["1","1","0","0","0"],
24+
// ["1","1","0","0","0"],
25+
// ["0","0","1","0","0"],
26+
// ["0","0","0","1","1"]
27+
// ]
28+
// Output: 3
29+
//
30+
//  
31+
// Constraints:
32+
//
33+
// m == grid.length
34+
// n == grid[i].length
35+
// 1 <= m, n <= 300
36+
// grid[i][j] is '0' or '1'.
37+
//
38+
//
39+
140
class Solution {
2-
private:
3-
unordered_set<int> visited;
4-
vector<vector<int>> directions = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
41+
public:
42+
int numIslands(vector<vector<char>>& grid) {
43+
int n = grid.size();
44+
int m = grid[0].size();
45+
int count = 0;
46+
int directions[] = {-1, 0, 1, 0, -1};
547

6-
bool in_bound(vector<vector<char>>& grid, int x, int y)
7-
{
8-
return x >= 0 && x < grid.size() && y >= 0 && y < grid[x].size();
9-
}
10-
11-
int convert(vector<vector<char>>& grid, int x, int y)
12-
{
13-
return x * grid[x].size() + y;
14-
}
15-
16-
void bfs(vector<vector<char>>& grid, int x, int y)
17-
{
18-
queue<pair<int, int>> q;
19-
q.push(make_pair(x, y));
20-
21-
while (!q.empty())
22-
{
23-
pair<int, int> tmp = q.front();
24-
q.pop();
25-
26-
for (auto i = 0; i < 4; ++i)
27-
{
28-
int new_x = tmp.first + directions[i][0];
29-
int new_y = tmp.second + directions[i][1];
30-
int point = convert(grid, new_x, new_y);
31-
if (in_bound(grid, new_x, new_y) && grid[new_x][new_y] == '1' && visited.count(point) == 0)
32-
{
33-
visited.insert(point);
34-
q.push(make_pair(new_x, new_y));
48+
for (int i = 0; i < n; i++) {
49+
for (int j = 0; j < m; j++) {
50+
if (grid[i][j] == '1') {
51+
queue<pair<int, int>> q;
52+
q.push(make_pair(i, j));
53+
grid[i][j] = '0';
54+
count++;
55+
while (!q.empty()) {
56+
pair<int, int> coords = q.front();
57+
q.pop();
58+
for (int k = 0; k < 4; k++) {
59+
int x = coords.first + directions[k];
60+
int y = coords.second + directions[k + 1];
61+
if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '1') {
62+
grid[x][y] = '0';
63+
q.push(make_pair(x, y));
64+
}
65+
}
66+
}
3567
}
3668
}
69+
}
70+
71+
return count;
72+
}
73+
};
74+
75+
class UnionFind {
76+
public:
77+
vector<int> table;
78+
UnionFind(int n) {
79+
table.resize(n);
80+
for (int i = 0; i < n; i++) {
81+
table[i] = i;
82+
}
83+
}
84+
85+
int find(int a) {
86+
if (a == table[a]) {
87+
return a;
88+
}
89+
90+
table[a] = find(table[a]);
91+
return table[a];
92+
}
93+
94+
bool connect(int a, int b) {
95+
int root_a = find(a);
96+
int root_b = find(b);
97+
if (root_a != root_b) {
98+
table[root_a] = root_b;
99+
return true;
37100
}
101+
return false;
38102
}
103+
};
104+
105+
class Solution {
39106
public:
40107
int numIslands(vector<vector<char>>& grid) {
41-
108+
int n = grid.size();
109+
int m = grid[0].size();
42110
int count = 0;
43-
for (auto i = 0; i < grid.size(); ++i)
44-
{
45-
for (auto j = 0; j < grid[i].size(); ++j)
46-
{
47-
if (grid[i][j] == '0')
48-
{
49-
continue;
50-
}
51-
52-
if (visited.count(convert(grid, i, j)) == 0)
53-
{
111+
int directions[] = {-1, 0, 1, 0, -1};
112+
UnionFind uf = UnionFind(n * m);
113+
114+
for (int i = 0; i < n; i++) {
115+
for (int j = 0; j < m; j++) {
116+
if (grid[i][j] == '1') {
54117
count++;
55-
bfs(grid, i, j);
56118
}
57119
}
58-
}
59-
120+
}
121+
122+
for (int i = 0; i < n; i++) {
123+
for (int j = 0; j < m; j++) {
124+
if (grid[i][j] == '1') {
125+
if (j + 1 < m && grid[i][j + 1] == '1' && uf.connect(i * m + j, i * m + j + 1)) {
126+
count--;
127+
}
128+
129+
if (i + 1 < n && grid[i + 1][j] == '1' && uf.connect(i * m + j, (i + 1) * m + j)) {
130+
count--;
131+
}
132+
}
133+
}
134+
}
135+
60136
return count;
61137
}
62138
};

0 commit comments

Comments
 (0)