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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more