Skip to content

Instantly share code, notes, and snippets.

@Ebazhanov
Created July 19, 2021 22:57
Show Gist options
  • Save Ebazhanov/9787491269c421d6e5099bfe41e8ea47 to your computer and use it in GitHub Desktop.
Save Ebazhanov/9787491269c421d6e5099bfe41e8ea47 to your computer and use it in GitHub Desktop.
function getRoute (tickets) {
const route = tickets.pop();
let i = 0;
while (i < tickets.length) {
if (route[0] === tickets[i][1]) {
route.unshift(tickets[i][0]);
i = 0;
} else if (route[route.length - 1] === tickets[i][0]) {
route.push(tickets[i][1]);
i = 0;
} else i++;
}
return route.toString();
}
const result = getRoute([["JPN", "PHL"], ["BRA", "UAE"], ["USA", "BRA"], ["UAE", "JPN"]]);
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment