Skip to content

Commit 84554cd

Browse files
author
Partho Biswas
committed
332. Reconstruct Itinerary
1 parent 40e23ef commit 84554cd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ BFS, DFS, Dijkstra, Union Find, Kruskal, Prim's, Minimum Spanning Tree, Topologi
425425
|16| **[126. Word Ladder II](https://tinyurl.com/jubpmll)** | [Python](https://tinyurl.com/wu6rdaw/126_Word_Ladder_II.py)| **[codinginterviewclass.com](https://tinyurl.com/trglttn)**, **[Official](https://tinyurl.com/y99azp8z)**, **[Vid 1](https://tinyurl.com/ws465le)** | Hard | **TODO: Not Done. Extremely tricky. Modified BFS. Learned [Bidirectional Search](https://tinyurl.com/vx3oqbf)** |
426426
|17| **[785. Is Graph Bipartite?](https://tinyurl.com/wvwqw2j)** | [Python](https://tinyurl.com/wu6rdaw/785_Is_Graph_Bipartite.py)| **[codinginterviewclass.com](https://tinyurl.com/s4qseoz)**, **[Official](https://tinyurl.com/rynbqly)** | Medium | **Important, Learned new things. Undirected Graph** |
427427
|18| **[133. Clone Graph](https://tinyurl.com/y42nwqz8)** | [Python](https://tinyurl.com/wu6rdaw/133_Clone_Graph.py)| **[codinginterviewclass.com](https://tinyurl.com/tnca94n)**, **[Official](https://tinyurl.com/usxjk49)** | Medium | **Important, Learned new things. Undirected Graph** |
428+
|19| **[332. Reconstruct Itinerary](https://tinyurl.com/gn2mv3k)** | [Python](https://tinyurl.com/wu6rdaw/332_Reconstruct_Itinerary.py)| [Vid 1](https://tinyurl.com/w47evd9), [Vid 2](https://tinyurl.com/uvop34p), [Art 1](https://tinyurl.com/tsy24pl), [Art 2](https://tinyurl.com/srazxo3), [Art 3](https://tinyurl.com/wluob5j), **[Art 4](https://tinyurl.com/y4s9crf6)** | Medium | **Important, Learned new things. Directed Graph. Eulerian path and top Sort** |
428429

429430

430431
</p>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Source: https://tinyurl.com/tsy24pl
2+
from collections import defaultdict
3+
class Solution(object):
4+
def findItinerary(self, tickets):
5+
"""
6+
:type tickets: List[List[str]]
7+
:rtype: List[str]
8+
"""
9+
graph = defaultdict(list)
10+
for source, destination in tickets:
11+
graph[source].append(destination)
12+
for source in graph.keys():
13+
graph[source].sort(reverse=True)
14+
stack = ["JFK",]
15+
result = []
16+
while stack:
17+
node = stack[-1]
18+
if node in graph and len(graph[node]) > 0:
19+
stack.append(graph[node].pop())
20+
else:
21+
result.append(stack.pop())
22+
return result[::-1]

0 commit comments

Comments
 (0)