Skip to content

Instantly share code, notes, and snippets.

@liron-navon
Last active October 14, 2021 06:35
Show Gist options
  • Save liron-navon/eabe077fe13d1409243046d972222982 to your computer and use it in GitHub Desktop.
Save liron-navon/eabe077fe13d1409243046d972222982 to your computer and use it in GitHub Desktop.
function newRoadSystem(roadRegister) {
// will be used to count ingoing and outgoing roads
const inComingRoads = new Array(roadRegister.length).fill(0);
const outGoingRoads = new Array(roadRegister.length).fill(0);
// we have to do a double loop to collect data from the 2D array
roadRegister.forEach((currentCity, currentCityIndex) => {
currentCity.forEach((hasRoadToTargetCity, targetCityIndex) => {
if(hasRoadToTargetCity) {
// count ingoing and outgoing roads
inComingRoads[targetCityIndex] = inComingRoads[targetCityIndex] + 1;
outGoingRoads[currentCityIndex] = outGoingRoads[currentCityIndex] + 1;
}
});
});
// make sure the same number of roads in and out of the city
return inComingRoads.every((inComingRoadsCount, cityIndex) => {
return outGoingRoads[cityIndex] === inComingRoadsCount;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment