Skip to content

Commit 6380c68

Browse files
committed
[LEET] 547 Number of Provinces (Medium)
1 parent 0ca7f27 commit 6380c68

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

이용훈/8주차/260218.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[][]} isConnected
3+
* @return {number}
4+
*/
5+
var findCircleNum = function(isConnected) {
6+
const n = isConnected.length;
7+
const parent = Array.from({ length: n }, (_, i) => i);
8+
9+
const find = (x) => {
10+
if(parent[x] !== x) parent[x] = find(parent[x]);
11+
return parent[x];
12+
}
13+
14+
const union = (a, b) => {
15+
const pa = find(a);
16+
const pb = find(b);
17+
18+
if(pa !== pb) parent[pb] = pa;
19+
}
20+
21+
for(let i = 0; i < n; i ++) {
22+
for(let j = i + 1; j < n; j++) {
23+
if(isConnected[i][j] === 1) {
24+
union(i, j);
25+
}
26+
}
27+
}
28+
29+
const sH = new Set();
30+
for(let i = 0; i < n; i++) {
31+
sH.add(find(i));
32+
}
33+
34+
return sH.size;
35+
};

0 commit comments

Comments
 (0)