//sample solution
function newRoadSystem(roadRegister) {
var outCount=[];
var inCount=[];
for(i=0;i<roadRegister.length;i++){
outCount.push(0);
inCount.push(0);
}
for(i=0;i<roadRegister.length;i++){
for(j=0;j<roadRegister[i].length;j++){
//if i=j, you are looking at the city going to itself, so skip
if(i==j){
continue;
} else if(roadRegister[i][j]==true){
//i is the source city & j is the destination city
outCount[i]++;
inCount[j]++;
}
}
}
//if by the end of the process, outCount and inCount for each city are equal, ok
var returnValue=true;
for(i=0;i<roadRegister.length;i++){
if(outCount[i]!=inCount[i]){
returnValue=false;
}
}
return returnValue;
}
No comments:
Post a Comment