Skip to content

Commit

Permalink
[Union Find] Add a solution to Number of Islands II
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Apr 20, 2018
1 parent b7bf7d3 commit e96f752
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -305,6 +305,7 @@
| ----- | -------- | ---------- | ---- | ----- |
[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)|
[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Swift](./UnionFind/GraphValidTree.swift)| Medium| O(nlogn)| O(n)|
[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)| [Swift](./UnionFind/NumberIslandsII.swift)| Hard| O(klogmn)| O(mn)|

## Google
| Title | Solution | Difficulty | Frequency |
Expand Down Expand Up @@ -507,7 +508,7 @@
| | 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) ♥ | Hard
| | 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | Medium
| | 306 | [Additive Number](https://leetcode.com/problems/additive-number/) | Medium
| | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) ♥ | Hard
| [Swift](./UnionFind/NumberIslandsII.swift) | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) ♥ | Hard
| | 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | Medium
| | 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | Easy
| | 302 | [Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/) ♥ | Hard
Expand Down
48 changes: 48 additions & 0 deletions UnionFind/NumberIslandsII.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Question Link: https://leetcode.com/problems/number-of-islands-ii/
* Primary idea: Classic Union Find, check four directions and update count every time
*
* Time Complexity: O(klogmn), Space Complexity: O(mn)
*
*/

class NumberIslandsII {
func numOfIslandsII(_ m: Int, _ n: Int, _ positions: [(Int, Int)]) -> [Int] {
var res = [Int](), count = 0, roots = Array(repeating: -1, count: m * n)

for position in positions {
var pos = position.0 * n + position.1
roots[pos] = pos
count += 1

for moveDir in [(0, 1), (0, -1), (1, 0), (-1, 0)] {
let i = position.0 + moveDir.0, j = position.1 + moveDir.1
let movePos = i * n + j

guard i >= 0 && i < m && j >= 0 && j < n && roots[movePos] != -1 else {
continue
}

let movePosRoot = findRoot(movePos, roots)

if movePosRoot != pos {
count -= 1
roots[pos] = movePosRoot
pos = movePosRoot
}
}

res.append(count)
}

return res
}

fileprivate func findRoot(_ node: Int, _ roots: [Int]) -> Int {
var node = node
while node != roots[node] {
node = roots[node]
}
return node
}
}

0 comments on commit e96f752

Please sign in to comment.