-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0332-reconstruct-itinerary.rs
31 lines (30 loc) · 1.01 KB
/
0332-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
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
impl Solution {
pub fn find_itinerary(tickets: Vec<Vec<String>>) -> Vec<String> {
let mut graph: HashMap<&str, BinaryHeap<Reverse<&str>>> = HashMap::new();
for ticket in tickets.iter() {
graph
.entry(&ticket[0])
.or_insert_with(BinaryHeap::new)
.push(Reverse(&ticket[1]));
}
let mut answer: Vec<String> = Vec::with_capacity(tickets.len() + 1);
let mut stack: Vec<&str> = vec!["JFK"];
while let Some(src) = stack.last() {
if let Some(dsts) = graph.get_mut(src) {
if !dsts.is_empty() {
if let Some(dst) = dsts.pop() {
stack.push(dst.0);
}
continue;
}
}
if let Some(last) = stack.pop() {
answer.push(last.to_string());
}
}
answer.reverse();
answer
}
}