We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0ca7f27 commit 6380c68Copy full SHA for 6380c68
이용훈/8주차/260218.js
@@ -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