Published
Edited
Dec 28, 2018
Insert cell
Insert cell
Insert cell
function manhattanDistance (a, b) {
let out = 0;
for (let i = 0; i < a.length; i++) {
out += Math.abs(a[i] - b[i]);
}
return out;
}
Insert cell
class UnionFind {
constructor (n) {
this.count = n;
this.id = {};
for (let i = 0; i < n; i++) {
this.id[i] = i;
}
}
find (a) {
while (a !== this.id[a]) {
const parentA = this.id[a];
this.id[a] = this.id[parentA];
a = this.id[a];
}
return a;
}
union (a, b) {
const rootA = this.find(a);
const rootB = this.find(b);
if (rootA === rootB) return;
this.id[rootA] = rootB;
this.count--;
}
}
Insert cell
part1 = {
let out = new UnionFind(DATA.length);
for (let i = 0; i < DATA.length; i++) {
for (let j = i + 1; j < DATA.length; j++) {
if (manhattanDistance(DATA[i], DATA[j]) <= 3) {
out.union(i, j);
}
}
}
return out.count;
}
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more