Skip to content

Commit aa888de

Browse files
committed
Added Partial Reconstruct Itinerary
1 parent 2bd295c commit aa888de

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

src/reconstruct_itinerary.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use std::collections::HashMap;
2+
use std::collections::HashSet;
3+
4+
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
5+
struct Ticket {
6+
from: String,
7+
to: String,
8+
}
9+
10+
impl Ticket {
11+
12+
fn new(from: String, to: String) -> Self {
13+
Self { from, to }
14+
}
15+
16+
fn from_vec(ticket: &Vec<String>) -> Self {
17+
Self { from: ticket[0].clone(), to: ticket[1].clone() }
18+
}
19+
20+
}
21+
22+
/// You are given a list of airline `tickets` where `tickets[i] = [fromi, toi]`
23+
/// represent the departure and arrival airports of one flight. Reconstruct
24+
/// the itinerary in order and return it.
25+
///
26+
/// All of the tickets belong to a man who departs from `"JFK"`, thus, the
27+
/// itinerary must begin with `"JFK"`. If there are multiple valid itineraries,
28+
/// you should retun the itinerary that has the smallest lexical order when
29+
/// read as a single string.
30+
///
31+
/// * For example, the itinerary `["JFK", "LGA"]` has a smaller lexical order
32+
/// than `["JFK", "LGB"]`.
33+
///
34+
/// You may assume all tickets form at least one valid itinerary. You must use
35+
/// all the tickets once and only once.
36+
struct Solution;
37+
38+
impl Solution {
39+
40+
fn to_map(tickets: &Vec<Vec<String>>) -> HashMap<String, HashSet<Ticket>> {
41+
let mut result = HashMap::new();
42+
for item in tickets {
43+
let ticket = Ticket::from_vec(item);
44+
let key = ticket.from.clone();
45+
result
46+
.entry(key)
47+
.or_insert(HashSet::new())
48+
.insert(ticket);
49+
}
50+
51+
result
52+
}
53+
54+
pub fn find_itinerary(tickets: Vec<Vec<String>>) -> Vec<String> {
55+
let mut results = Vec::new();
56+
57+
let tickets = Self::to_map(&tickets);
58+
let city = "JFK".to_string();
59+
let path = vec![];
60+
let seen = HashSet::new();
61+
Self::worker(&tickets, city, path, seen, &mut results);
62+
63+
// TODO: Take lexical order result
64+
vec![]
65+
}
66+
67+
// TODO: Finish Implementation
68+
fn worker(
69+
_tickets: &HashMap<String, HashSet<Ticket>>,
70+
_city: String,
71+
_path: Vec<String>,
72+
_seen: HashSet<Ticket>,
73+
_results: &mut Vec<Vec<String>>
74+
) {
75+
76+
}
77+
78+
// pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
79+
// let mut results = Vec::new();
80+
// Self::worker(&graph, 0, vec![], &mut results);
81+
// results
82+
// }
83+
84+
// fn worker(graph: &Vec<Vec<i32>>, node: i32, path: Vec<i32>, results: &mut Vec<Vec<i32>>) {
85+
// let n = graph.len();
86+
// let i = node as usize;
87+
// let mut path = path;
88+
// path.push(node);
89+
// if i == n - 1 {
90+
// results.push(path);
91+
// } else {
92+
// for &next in &graph[i] {
93+
// let cloned_path = path.clone();
94+
// Self::worker(graph, next, cloned_path, results);
95+
// }
96+
// }
97+
// }
98+
99+
}

0 commit comments

Comments
 (0)