Quotes help make search much faster. Example: "Practice Makes Perfect"

Wednesday, December 14, 2016

Codefights newRoadSystem


//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


This is an example of scrolling text using Javascript.

Popular Posts