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
[
[
1,
1,
0
],
[
1,
1,
0
],
[
0,
0,
1
]
]
Execution steps
Explore connected province
Computed output
2
JavaScript reference implementation
const solve = (a,s)=>{
let seen=new Set,c=0;
for(let i=0;
i<a.length;
i++)if(!seen.has(i)){
c++;
let q=[i];
seen.add(i);
while(q.length){
let u=q.shift();
for(let v=0;
v<a.length;
v++)if(a[u][v]&&!seen.has(v)){
seen.add(v);
q.push(v)
}
s("Explore connected province",{
Matrix:a,Queue:q,Visited:[...seen],Count:[c]
})
}
}
return c
};