【路飞】移除最多的同行或同列石头

155 阅读1分钟

题目:947. 移除最多的同行或同列石头

image.png

解析

image.png

如上图所示,将同行或者同列的石头连通,最终剩下的石头的数量等于集合的数量,可以移除的石子的最大数量就是石头总数量减去集合数量。

function removeStones(stones) {
  const len = stones.length;
  const uf = new UF(len);
  for (let i = 0; i < len; i++) {
    for (let j = i + 1; j < len; j++) {
      // 同行或同列
      if (stones[i][0] === stones[j][0] || stones[i][1] === stones[j][1]) {
        uf.union(i, j);
      }
    }
  }
  return len - uf.count;
}

class UF {
  count = 0;
  size = [];
  parent = [];
  constructor(n) {
    this.count = n;
    this.parent = [];
    this.size = [];
    for (let i = 0; i < n; i++) {
      this.parent[i] = i;
      this.size[i] = 1;
    }
  }

  union(x, y) {
    const xr = this.find(x);
    const yr = this.find(y);
    if (xr === yr) return;
    // 小树接到大树下面,较平衡
    if (this.size[xr] < this.size[yr]) {
      this.parent[xr] = yr;
      this.size[yr] += this.size[xr];
    } else {
      this.parent[yr] = xr;
      this.size[xr] += this.size[yr];
    }
    this.count--;
  }

  isConnected = (x, y) => this.find(x) === this.find(y);

  find(x) {
    while (x !== this.parent[x]) {
      this.parent[x] = this.parent[this.parent[x]];
      x = this.parent[x];
    }
    return x;
  }

  count() {
    return this.count;
  }
}