Skip to content

Commit 95edd69

Browse files
committed
Solution of Reconstruct Itenary
1 parent 399230d commit 95edd69

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

300-400q/332.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
3+
4+
Note:
5+
6+
If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
7+
All airports are represented by three capital letters (IATA code).
8+
You may assume all tickets form at least one valid itinerary.
9+
Example 1:
10+
11+
Input: tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
12+
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
13+
'''
14+
15+
from collections import defaultdict
16+
class Solution(object):
17+
def findItinerary(self, tickets):
18+
"""
19+
:type tickets: List[List[str]]
20+
:rtype: List[str]
21+
"""
22+
n = len(tickets)
23+
trips = defaultdict(list)
24+
for x in tickets:
25+
trips[x[0]].append(x[1])
26+
for x in trips:
27+
trips[x].sort()
28+
iter = ["JFK"]
29+
30+
def dfs(curr_iter):
31+
if len(curr_iter) == n+1:
32+
return curr_iter
33+
curr_stop = curr_iter[-1]
34+
35+
if trips[curr_stop] == []:
36+
return None
37+
38+
next_stops = trips[curr_stop]
39+
i = 0
40+
for stop in next_stops:
41+
curr_iter.append(stop)
42+
del trips[curr_stop][i]
43+
44+
if dfs(curr_iter):
45+
return curr_iter
46+
47+
curr_iter.pop()
48+
trips[curr_stop].insert(i, stop)
49+
i += 1
50+
return None
51+
52+
return dfs(iter)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1313
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [Python](./300-400q/350.py)|Easy|
1414
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Python](./300-400q/347.py)|Medium|
1515
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [Python](./300-400q/334.py)|Medium|
16+
|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/)|[Python](./300-400q/332.py)|Medium|
1617
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [Python](./300-400q/329.py)|Medium|
1718
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) | [Python](./300-400q/328.py)|Easy|
1819
|326|[Power of Three](https://leetcode.com/problems/power-of-three/) | [Python](./300-400q/326.py)|Easy|

0 commit comments

Comments
 (0)