Workspace Most Stones Removed
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Most Stones Removed visualizer

Learn Most Stones Removed with a step-by-step Graphs visualizer, JavaScript source code and a worked example. Practice mst, dsu and advanced.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    0,
    0
  ],
  [
    0,
    1
  ],
  [
    1,
    0
  ],
  [
    1,
    2
  ],
  [
    2,
    1
  ],
  [
    2,
    2
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
let d=dsu(a.length),c=a.length;
for(let i=0;
i<a.length;
i++)for(let j=0;
j<i;
j++)if(a[i][0]===a[j][0]||a[i][1]===a[j][1]){
if(d.union(i,j))c--;
s("Join stones sharing row or column",{
Stones:a,Parents:d.p,Components:[c]
})
}
return a.length-c
};
function adj(n,edges,directed=false){
let a=Array.from({
length:n
},()=>[]);
for(let [u,v,w=1] of edges){
a[u].push([v,w]);
if(!directed)a[v].push([u,w])
}
return a
}
function dsu(n){
let p=Array.from({
length:n
},(_,i)=>i),z=p.map(()=>1);
function find(x){
while(p[x]!==x){
p[x]=p[p[x]];
x=p[x]
}
return x
}
function union(a,b){
a=find(a);
b=find(b);
if(a===b)return false;
if(z[a]<z[b])[a,b]=[b,a];
p[b]=a;
z[a]+=z[b];
return true
}
return {
p,z,find,union
}
}
const dirs=[[1,0],[-1,0],[0,1],[0,-1]];

Execution canvas

Active Changed
STEP 1

Join stones sharing row or column

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Stones6 rows
Parents6 items
Components1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Graphs · MST, DSU and advanced

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

[
  [
    0,
    0
  ],
  [
    0,
    1
  ],
  [
    1,
    0
  ],
  [
    1,
    2
  ],
  [
    2,
    1
  ],
  [
    2,
    2
  ]
]

Execution steps

  1. Join stones sharing row or column

Computed output

5
JavaScript reference implementation
const solve = (a,s)=>{
  let d=dsu(a.length),c=a.length;
  for(let i=0;
  i<a.length;
  i++)for(let j=0;
  j<i;
  j++)if(a[i][0]===a[j][0]||a[i][1]===a[j][1]){
    if(d.union(i,j))c--;
    s("Join stones sharing row or column",{
      Stones:a,Parents:d.p,Components:[c]
    })
  }
  return a.length-c
};
function adj(n,edges,directed=false){
  let a=Array.from({
    length:n
  },()=>[]);
  for(let [u,v,w=1] of edges){
    a[u].push([v,w]);
    if(!directed)a[v].push([u,w])
  }
  return a
}
function dsu(n){
  let p=Array.from({
    length:n
  },(_,i)=>i),z=p.map(()=>1);
  function find(x){
    while(p[x]!==x){
      p[x]=p[p[x]];
      x=p[x]
    }
    return x
  }
  function union(a,b){
    a=find(a);
    b=find(b);
    if(a===b)return false;
    if(z[a]<z[b])[a,b]=[b,a];
    p[b]=a;
    z[a]+=z[b];
    return true
  }
  return {
    p,z,find,union
  }
}
const dirs=[[1,0],[-1,0],[0,1],[0,-1]];

Continue learning

Practice related DSA questions in the same topic.