Skip to content

Commit e96f752

Browse files
committed
[Union Find] Add a solution to Number of Islands II
1 parent b7bf7d3 commit e96f752

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 237 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 238 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -305,6 +305,7 @@
305305
| ----- | -------- | ---------- | ---- | ----- |
306306
[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)| [Swift](./UnionFind/NumberConnectedComponentsUndirectedGraph.swift)| Medium| O(nlogn)| O(n)|
307307
[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Swift](./UnionFind/GraphValidTree.swift)| Medium| O(nlogn)| O(n)|
308+
[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)| [Swift](./UnionFind/NumberIslandsII.swift)| Hard| O(klogmn)| O(mn)|
308309

309310
## Google
310311
| Title | Solution | Difficulty | Frequency |
@@ -507,7 +508,7 @@
507508
| | 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) ♥ | Hard
508509
| | 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | Medium
509510
| | 306 | [Additive Number](https://leetcode.com/problems/additive-number/) | Medium
510-
| | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) ♥ | Hard
511+
| [Swift](./UnionFind/NumberIslandsII.swift) | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) ♥ | Hard
511512
| | 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | Medium
512513
| | 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | Easy
513514
| | 302 | [Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/) ♥ | Hard

UnionFind/NumberIslandsII.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/number-of-islands-ii/
3+
* Primary idea: Classic Union Find, check four directions and update count every time
4+
*
5+
* Time Complexity: O(klogmn), Space Complexity: O(mn)
6+
*
7+
*/
8+
9+
class NumberIslandsII {
10+
func numOfIslandsII(_ m: Int, _ n: Int, _ positions: [(Int, Int)]) -> [Int] {
11+
var res = [Int](), count = 0, roots = Array(repeating: -1, count: m * n)
12+
13+
for position in positions {
14+
var pos = position.0 * n + position.1
15+
roots[pos] = pos
16+
count += 1
17+
18+
for moveDir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
19+
let i = position.0 + moveDir.0, j = position.1 + moveDir.1
20+
let movePos = i * n + j
21+
22+
guard i >= 0 && i < m && j >= 0 && j < n && roots[movePos] != -1 else {
23+
continue
24+
}
25+
26+
let movePosRoot = findRoot(movePos, roots)
27+
28+
if movePosRoot != pos {
29+
count -= 1
30+
roots[pos] = movePosRoot
31+
pos = movePosRoot
32+
}
33+
}
34+
35+
res.append(count)
36+
}
37+
38+
return res
39+
}
40+
41+
fileprivate func findRoot(_ node: Int, _ roots: [Int]) -> Int {
42+
var node = node
43+
while node != roots[node] {
44+
node = roots[node]
45+
}
46+
return node
47+
}
48+
}

0 commit comments

Comments
 (0)