(tickets, graph, city = 'JFK', path = ['JFK'])
| 12 | }; |
| 13 | |
| 14 | const dfs = (tickets, graph, city = 'JFK', path = ['JFK']) => { |
| 15 | const isBaseCase = path.length === tickets.length + 1; |
| 16 | if (isBaseCase) return true; |
| 17 | |
| 18 | const queue = graph.get(city) || []; |
| 19 | |
| 20 | const isEmpty = queue.length === 0; |
| 21 | if (isEmpty) return false; |
| 22 | |
| 23 | for (const nextCity of queue.slice()) { |
| 24 | path.push(nextCity); |
| 25 | queue.shift(); |
| 26 | |
| 27 | if (dfs(tickets, graph, nextCity, path)) return path; |
| 28 | |
| 29 | path.pop(); |
| 30 | queue.push(nextCity); |
| 31 | } |
| 32 | |
| 33 | return false; |
| 34 | }; |
| 35 | |
| 36 | const buildGraph = (tickets, graph = new Map()) => { |
| 37 | for (const [src, dst] of tickets) { |
no test coverage detected