2
* @param {number[][]} points6
let union_find, mapping_arr, count, maxCost, rank;7
const find = (root) => {8
if (union_find[root] === root) return root;9
return (union_find[root] = find(union_find[root]));11
const union = ([src, dst, d]) => {12
let rootX = find(src);13
let rootY = find(dst);15
if (rank[rootX] < rank[rootY]) {16
union_find[rootX] = rootY;17
} else if (rank[rootX] > rank[rootY]) {18
union_find[rootY] = rootX;20
union_find[rootY] = rootX;27
var minCostConnectPoints = function (points) {32
count = points.length;33
for (let i = 0; i < points.length; i++) {37
for (let j = i + 1; j < points.length; j++) {39
let d = Math.abs(src[0] - dst[0]) + Math.abs(src[1] - dst[1]);40
edges.push([i, j, d]);43
edges.sort((a, b) => a[2] - b[2]);44
for (let i = 0; i < edges.length; i++) {45
if (count > 1) union(edges[i]);