Skip to content

Commit e8730aa

Browse files
committed
Completed Reconstruct Itinerary
1 parent 421ff73 commit e8730aa

File tree

2 files changed

+42
-55
lines changed

2 files changed

+42
-55
lines changed

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ pub mod power_of_three; // 326
340340
pub mod odd_even_linked_list; // 328 ✓
341341
pub mod longest_increasing_path_in_a_matrix; // 329 ✓
342342

343-
pub mod reconstruct_itinerary; // 332
343+
pub mod reconstruct_itinerary; // 332
344344

345345
pub mod increasing_triplet_subsequence; // 334 ✓
346346

Diff for: src/reconstruct_itinerary.rs

+41-54
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use std::cmp::Reverse;
2+
use std::collections::BinaryHeap;
13
use std::collections::HashMap;
2-
use std::collections::HashSet;
34

45
/// You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]`
56
/// represent the departure and arrival airports of one flight. Reconstruct
@@ -19,65 +20,35 @@ struct Solution;
1920

2021
impl Solution {
2122

22-
fn to_adj_map(tickets: Vec<Vec<String>>) -> HashMap<String, HashSet<String>> {
23-
let mut results = HashMap::new();
24-
25-
for ticket in tickets {
26-
results
27-
.entry(ticket[0].clone())
28-
.or_insert(HashSet::new())
29-
.insert(ticket[1].clone());
30-
}
31-
32-
results
33-
}
34-
35-
fn remove(map: &mut HashMap<String, HashSet<String>>, key: &String, value: &String) {
36-
if map.contains_key(key) {
37-
let set = map.get_mut(key).unwrap();
38-
if set.len() == 1 {
39-
map.remove(key);
40-
} else {
41-
set.remove(value);
42-
}
43-
}
44-
}
45-
46-
fn worker(
47-
results: &mut Vec<String>,
48-
map: HashMap<String, HashSet<String>>,
49-
path: Vec<String>,
50-
current: String
23+
fn dfs(
24+
result: &mut Vec<String>,
25+
adj_map: &mut HashMap<String, BinaryHeap<Reverse<String>>>,
26+
value: &str
5127
) {
52-
if map.len() == 0 {
53-
if results.len() == 0 {
54-
*results = path.clone();
55-
} else if &path < &results{
56-
*results = path.clone();
57-
}
58-
} else if map.contains_key(&current) {
59-
for next in &map[&current] {
60-
let mut next_path = path.clone();
61-
next_path.push(next.clone());
62-
let mut map = map.clone();
63-
Self::remove(&mut map, &current, next);
64-
Self::worker(results, map, next_path, next.clone());
65-
}
28+
while let Some(Reverse(next)) = adj_map.get_mut(value).and_then(|dests| dests.pop()) {
29+
Self::dfs(result, adj_map, &next);
6630
}
31+
result.push(value.to_string());
6732
}
6833

69-
// TODO: Improve
70-
// Simple solution that I know if submitted will fail for time limit exceeded.
7134
pub fn find_itinerary(tickets: Vec<Vec<String>>) -> Vec<String> {
72-
let mut results = Vec::new();
35+
let mut result = vec![];
36+
let mut adj_map: HashMap<String, BinaryHeap<Reverse<String>>> = HashMap::new();
7337

74-
let map = Self::to_adj_map(tickets);
75-
let initial = vec!["JFK".to_string()];
76-
let current = "JFK".to_string();
38+
for ticket in tickets {
39+
let source = ticket[0].clone();
40+
let destination = ticket[1].clone();
41+
42+
adj_map
43+
.entry(source)
44+
.or_insert(BinaryHeap::new())
45+
.push(Reverse(destination));
46+
}
7747

78-
Self::worker(&mut results, map, initial, current);
48+
Self::dfs(&mut result, &mut adj_map, "JFK");
7949

80-
results
50+
result.reverse();
51+
result
8152
}
8253

8354
}
@@ -86,7 +57,6 @@ impl Solution {
8657
mod tests {
8758
use super::Solution;
8859

89-
#[ignore]
9060
#[test]
9161
fn example_1() {
9262
let tickets = vec![
@@ -99,7 +69,6 @@ mod tests {
9969
assert_eq!(result, vec!["JFK", "MUC", "LHR", "SFO", "SJC"]);
10070
}
10171

102-
#[ignore]
10372
#[test]
10473
fn example_2() {
10574
let tickets = vec![
@@ -113,4 +82,22 @@ mod tests {
11382
assert_eq!(result, vec!["JFK", "ATL", "JFK", "SFO", "ATL", "SFO"]);
11483
}
11584

85+
#[test]
86+
fn example_3() {
87+
let tickets = vec![
88+
vec![str!("EZE"), str!("AXA")],
89+
vec![str!("TIA"), str!("ANU")],
90+
vec![str!("ANU"), str!("JFK")],
91+
vec![str!("JFK"), str!("ANU")],
92+
vec![str!("ANU"), str!("EZE")],
93+
vec![str!("TIA"), str!("ANU")],
94+
vec![str!("AXA"), str!("TIA")],
95+
vec![str!("TIA"), str!("JFK")],
96+
vec![str!("ANU"), str!("TIA")],
97+
vec![str!("JFK"), str!("TIA")],
98+
];
99+
let result = Solution::find_itinerary(tickets);
100+
assert_eq!(result, vec!["JFK","ANU","EZE","AXA","TIA","ANU","JFK","TIA","ANU","TIA","JFK"]);
101+
}
102+
116103
}

0 commit comments

Comments
 (0)