Skip to content

Commit 3f36528

Browse files
authored
Create 3608-minimum-time-for-k-connected-components.js
1 parent 78376f9 commit 3f36528

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var minTime = function (n, edges, k) {
8+
let f = Array.from({ length: n }, (_, i) => i)
9+
10+
edges.sort((a, b) => b[2] - a[2])
11+
let count = n
12+
for (const [u, v, t] of edges) {
13+
if (union(u, v)) {
14+
count -= 1
15+
}
16+
if (count < k) {
17+
return t
18+
}
19+
}
20+
return 0
21+
22+
function find(x) {
23+
if (x === f[x]) {
24+
return x
25+
}
26+
f[x] = find(f[x])
27+
return f[x]
28+
}
29+
30+
function union(x, y) {
31+
x = find(x)
32+
y = find(y)
33+
if (x === y) {
34+
return false
35+
}
36+
f[x] = y
37+
return true
38+
}
39+
}

0 commit comments

Comments
 (0)