Skip to content

Commit 14572e1

Browse files
committed
LC 547. Number of Provinces (Rust DSU)
1 parent 0e22d37 commit 14572e1

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
261261
| [540. Single Element in a Sorted Array][lc540] | 🟠 Medium | [![python](res/py.png)][lc540py] [![rust](res/rs.png)][lc540rs] |
262262
| [542. 01 Matrix][lc542] | 🟠 Medium | [![python](res/py.png)][lc542py] [![rust](res/rs.png)][lc542rs] |
263263
| [543. Diameter of Binary Tree][lc543] | 🟢 Easy | [![python](res/py.png)][lc543py] |
264+
| [547. Number of Provinces][lc547] | 🟠 Medium | [![rust](res/rs.png)][lc547rs] |
264265
| [557. Reverse Words in a String III][lc557] | 🟢 Easy | [![python](res/py.png)][lc557py] |
265266
| [560. Subarray Sum Equals K][lc560] | 🟠 Medium | [![python](res/py.png)][lc560py] |
266267
| [567. Permutation in String][lc567] | 🟠 Medium | [![python](res/py.png)][lc567py] |
@@ -1085,6 +1086,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
10851086
[lc542rs]: leetcode/01-matrix.rs
10861087
[lc543]: https://leetcode.com/problems/diameter-of-binary-tree/
10871088
[lc543py]: leetcode/diameter-of-binary-tree.py
1089+
[lc547]: https://leetcode.com/problems/number-of-provinces/
1090+
[lc547rs]: leetcode/number-of-provinces.rs
10881091
[lc557]: https://leetcode.com/problems/reverse-words-in-a-string-iii/
10891092
[lc557py]: leetcode/reverse-words-in-a-string-iii.py
10901093
[lc560]: https://leetcode.com/problems/subarray-sum-equals-k/
@@ -1679,7 +1682,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
16791682
[lc2632]: https://leetcode.com/problems/curry/
16801683
[lc2632js]: leetcode/curry.js
16811684
[lc2633]: https://leetcode.com/problems/convert-object-to-json-string/
1682-
[lc2633.js]: leetcode/convert-object-to-json-string.js
1685+
[lc2633js]: leetcode/convert-object-to-json-string.js
16831686
[lc2634]: https://leetcode.com/problems/filter-elements-from-array/
16841687
[lc2634js]: leetcode/filter-elements-from-array.js
16851688
[lc2635]: https://leetcode.com/problems/apply-transform-over-each-element-in-array/

leetcode/number-of-provinces.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// 547. Number of Provinces
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/number-of-provinces/
5+
//
6+
// Tags: Depth-First Search - Breadth-First Search - Union Find - Graph
7+
8+
use std::collections::HashSet;
9+
10+
struct Solution {}
11+
impl Solution {
12+
/// Use union find to create a DSU structure, the number of disjoint
13+
/// components is the number of provinces.
14+
///
15+
/// Time complexity: O(n^2) - We iterate over every pair of values i and j
16+
/// and, when connected, we add them to the DSU in amortized O(1) time.
17+
/// Space complexity: O(n) - Both the parents and rank arrays are of size
18+
/// n, is_connected is given as an input parameter.
19+
///
20+
/// Runtime 0 ms Beats 100%
21+
/// Memory 2.2 MB Beats 80.65%
22+
pub fn find_circle_num(is_connected: Vec<Vec<i32>>) -> i32 {
23+
let n = is_connected.len();
24+
let mut parents = (0..n).into_iter().collect::<Vec<usize>>();
25+
let mut rank = vec![1; n];
26+
fn find_parent(i: usize, parents: &mut Vec<usize>) -> usize {
27+
if parents[i] != i {
28+
parents[i] = find_parent(parents[i], parents);
29+
}
30+
parents[i]
31+
}
32+
fn union(a: usize, b: usize, parents: &mut Vec<usize>, rank: &mut Vec<usize>) {
33+
let pa = find_parent(a, parents);
34+
let pb = find_parent(b, parents);
35+
if rank[pa] > rank[pb] {
36+
parents[pb] = pa;
37+
rank[pa] += rank[pb];
38+
} else {
39+
parents[pa] = pb;
40+
rank[pb] += rank[pa];
41+
}
42+
}
43+
for i in 0..n {
44+
for j in i + 1..n {
45+
if is_connected[i][j] == 1
46+
&& find_parent(i, &mut parents) != find_parent(j, &mut parents)
47+
{
48+
union(i, j, &mut parents, &mut rank);
49+
}
50+
}
51+
}
52+
(0..n)
53+
.map(|x| find_parent(x, &mut parents))
54+
.collect::<HashSet<usize>>()
55+
.len() as i32
56+
}
57+
}
58+
59+
fn main() {
60+
let tests = [
61+
(vec![vec![1, 1, 0], vec![1, 1, 0], vec![0, 0, 1]], 2),
62+
(vec![vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1]], 3),
63+
];
64+
for t in tests {
65+
assert_eq!(Solution::find_circle_num(t.0), t.1);
66+
}
67+
println!("\x1b[92m» All tests passed!\x1b[0m")
68+
}

0 commit comments

Comments
 (0)