-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconstruct_itinerary.rs
103 lines (89 loc) · 3.3 KB
/
reconstruct_itinerary.rs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::collections::HashMap;
/// You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]`
/// represent the departure and 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 retun 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.
struct Solution;
impl Solution {
fn dfs(
result: &mut Vec<String>,
adj_map: &mut HashMap<String, BinaryHeap<Reverse<String>>>,
value: &str
) {
while let Some(Reverse(next)) = adj_map.get_mut(value).and_then(|dests| dests.pop()) {
Self::dfs(result, adj_map, &next);
}
result.push(value.to_string());
}
pub fn find_itinerary(tickets: Vec<Vec<String>>) -> Vec<String> {
let mut result = vec![];
let mut adj_map: HashMap<String, BinaryHeap<Reverse<String>>> = HashMap::new();
for ticket in tickets {
let source = ticket[0].clone();
let destination = ticket[1].clone();
adj_map
.entry(source)
.or_insert(BinaryHeap::new())
.push(Reverse(destination));
}
Self::dfs(&mut result, &mut adj_map, "JFK");
result.reverse();
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let tickets = vec![
vec!["MUC".to_string(), "LHR".to_string()],
vec!["JFK".to_string(), "MUC".to_string()],
vec!["SFO".to_string(), "SJC".to_string()],
vec!["LHR".to_string(), "SFO".to_string()],
];
let result = Solution::find_itinerary(tickets);
assert_eq!(result, vec!["JFK", "MUC", "LHR", "SFO", "SJC"]);
}
#[test]
fn example_2() {
let tickets = vec![
vec!["JFK".to_string(), "SFO".to_string()],
vec!["JFK".to_string(), "ATL".to_string()],
vec!["SFO".to_string(), "ATL".to_string()],
vec!["ATL".to_string(), "JFK".to_string()],
vec!["ATL".to_string(), "SFO".to_string()],
];
let result = Solution::find_itinerary(tickets);
assert_eq!(result, vec!["JFK", "ATL", "JFK", "SFO", "ATL", "SFO"]);
}
#[test]
fn example_3() {
let tickets = vec![
vec![str!("EZE"), str!("AXA")],
vec![str!("TIA"), str!("ANU")],
vec![str!("ANU"), str!("JFK")],
vec![str!("JFK"), str!("ANU")],
vec![str!("ANU"), str!("EZE")],
vec![str!("TIA"), str!("ANU")],
vec![str!("AXA"), str!("TIA")],
vec![str!("TIA"), str!("JFK")],
vec![str!("ANU"), str!("TIA")],
vec![str!("JFK"), str!("TIA")],
];
let result = Solution::find_itinerary(tickets);
assert_eq!(result, vec!["JFK","ANU","EZE","AXA","TIA","ANU","JFK","TIA","ANU","TIA","JFK"]);
}
}