-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0332-reconstruct-itinerary.rb
73 lines (60 loc) · 2.43 KB
/
0332-reconstruct-itinerary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true
# 332. Reconstruct Itinerary
# Hard
# https://leetcode.com/problems/reconstruct-itinerary
=begin
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.
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.
For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
Example 1:
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
Output: ["JFK","MUC","LHR","SFO","SJC"]
Example 2:
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
Constraints:
* 1 <= tickets.length <= 300
* tickets[i].length == 2
* fromi.length == 3
* toi.length == 3
* fromi and toi consist of uppercase English letters.
* fromi != toi
=end
# @param {String[][]} tickets
# @return {String[]}
def find_itinerary(tickets)
graph = Hash.new { |hash, key| hash[key] = [] }
# Build the graph.
tickets.each do |from, to|
graph[from] << to
end
# Sort each adjacency list in reverse lexical order.
graph.each_value { |neighbors| neighbors.sort!.reverse! }
# DFS.
itinerary = []
stack = ["JFK"]
while !stack.empty?
airport = stack.last
if graph[airport].empty?
# If no outgoing edge, add to itinerary.
itinerary << stack.pop
else
# Otherwise, visit the next unvisited neighbor.
stack << graph[airport].pop
end
end
# Reverse the itinerary to get the final path.
itinerary.reverse
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_find_itinerary < Test::Unit::TestCase
def test_
assert_equal ["JFK", "MUC", "LHR", "SFO", "SJC"], find_itinerary([["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]])
assert_equal ["JFK", "ATL", "JFK", "SFO", "ATL", "SFO"], find_itinerary([["JFK", "SFO"], ["JFK", "ATL"], ["SFO", "ATL"], ["ATL", "JFK"], ["ATL", "SFO"]])
end
end