Skip to content

Commit 063228d

Browse files
committed
Add medium problem.
1 parent d40a548 commit 063228d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
# RECONSTRUCT ITINERARY
3+
4+
You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
5+
6+
All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
7+
8+
For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
9+
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
10+
11+
Example 1:
12+
13+
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
14+
Output: ["JFK","MUC","LHR","SFO","SJC"]
15+
16+
Example 2:
17+
18+
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
19+
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
20+
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
21+
22+
Constraints:
23+
24+
1 <= tickets.length <= 300
25+
tickets[i].length == 2
26+
from.length == 3
27+
to.length == 3
28+
from and to consist of uppercase English letters.
29+
from != to
30+
"""
31+
32+
class Solution:
33+
def findItinerary(self, tickets):
34+
neighborsList = {}
35+
remainingPlaces = {}
36+
37+
for ticket in tickets:
38+
start, end = ticket
39+
string = self.convert(start, end)
40+
if start not in neighborsList:
41+
neighborsList[start] = []
42+
neighborsList[start].append(end)
43+
if string not in remainingPlaces:
44+
remainingPlaces[string] = 0
45+
remainingPlaces[string] += 1
46+
47+
for edges in neighborsList:
48+
neighborsList[edges].sort()
49+
50+
return self.dfs("JFK", neighborsList, ["JFK"], len(tickets), remainingPlaces)
51+
52+
def dfs(self, current, neighbors, itinerary, n, remainingPlaces):
53+
if len(itinerary) == n + 1:
54+
return itinerary
55+
56+
if current not in neighbors:
57+
return False
58+
59+
for destination in neighbors[current]:
60+
journey = self.convert(current, destination)
61+
if journey not in remainingPlaces or remainingPlaces[journey] == 0:
62+
continue
63+
remainingPlaces[journey] -= 1
64+
itinerary.append(destination)
65+
check = self.dfs(destination, neighbors, itinerary, n, remainingPlaces)
66+
if check != False:
67+
return check
68+
remainingPlaces[journey] += 1
69+
itinerary.pop()
70+
return False
71+
72+
def convert(self, a, b):
73+
return str(a) + "-" + str(b)
74+
75+
76+
77+
78+

Leetcode/script.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
difficulty = input("Enter difficulty: ")
3+
os.system("git add .")
4+
os.system("git commit -m \"Add " + difficulty + " problem.\"")
5+
os.system("git push -u origin master")

0 commit comments

Comments
 (0)