Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. class Solution(object):
  2. def findItinerary(self, tickets):
  3. graph = collections.defaultdict(dict)
  4. for u, v in tickets:
  5. graph[u][v] = graph[u].get(v, 0) + 1
  6.  
  7. def search(node, path):
  8. if len(path) == len(tickets) + 1:
  9. return path
  10. for nei in sorted(graph[node]):
  11. if graph[node][nei]:
  12. graph[node][nei] -= 1
  13. ans = search(nei, path + [nei])
  14. if ans: return ans
  15. graph[node][nei] += 1
  16.  
  17. return search('JFK', ['JFK'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement