Skip to content

Commit

Permalink
Create: 0332-reconstruct-itinerary.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmzfzl authored Sep 13, 2023
1 parent 9f43422 commit 474f9a4
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rust/0332-reconstruct-itinerary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}

0 comments on commit 474f9a4

Please sign in to comment.