classUnionFind { parent: number[] = []; rank: number[] = []; count: number = 0; constructor(nums: number[][]) { const m = nums.length; const n = nums[0].length;
for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { this.rank[i * n + j] = 0; if (nums[i][j]) { this.parent[i * n + j] = i * n + j; this.count++; } } } }
find(num: number): number { if (this.parent[num] !== num) { this.parent[num] = this.find(this.parent[num]); } returnthis.parent[num]; }