Skip to content

Commit a2b94ae

Browse files
committed
Merge branch 'master' of github.com:ryanoneill/rust-leetcode
2 parents 4ef4879 + da32278 commit a2b94ae

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub mod remove_duplicates_from_sorted_array_ii; // 80
5959
pub mod remove_duplicates_from_sorted_list_ii; // 82
6060
pub mod remove_duplicates_from_sorted_list; // 83
6161

62+
pub mod merge_sorted_array; // 88
63+
6264
pub mod reverse_linked_list_ii; // 92
6365

6466
pub mod validate_binary_search_tree; // 98
@@ -126,6 +128,8 @@ pub mod bulb_switcher; // 319
126128
pub mod coin_change; // 322
127129
pub mod number_of_connected_components_in_an_undirected_graph; // 323
128130

131+
pub mod reconstruct_itinerary; // 332
132+
129133
pub mod reverse_string; // 344
130134

131135
pub mod moving_average_from_data_stream; // 346

src/merge_sorted_array.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
5+
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, n: i32) {
6+
let m = m as usize;
7+
let n = n as usize;
8+
9+
if n == 0 { } // do nothing
10+
else if m == 0 {
11+
for i in 0..n {
12+
nums1[i] = nums2[i];
13+
}
14+
} else {
15+
let mut nums1_i = m-1;
16+
let mut nums2_i = n-1;
17+
let mut nums1_done = false;
18+
let mut nums2_done = false;
19+
20+
let total = m + n;
21+
22+
for i in 0..total {
23+
let index = total-i-1;
24+
if nums2_done {
25+
nums1[index] = nums1[nums1_i];
26+
if nums1_i == 0 { nums1_done = true; }
27+
else { nums1_i -= 1; }
28+
} else if nums1_done {
29+
nums1[index] = nums2[nums2_i];
30+
if nums2_i == 0 { nums2_done = true; }
31+
else { nums2_i -= 1; }
32+
} else {
33+
let num1 = nums1[nums1_i];
34+
let num2 = nums2[nums2_i];
35+
if num2 > num1 {
36+
nums1[index] = num2;
37+
if nums2_i == 0 { nums2_done = true; }
38+
else { nums2_i -= 1; }
39+
} else {
40+
nums1[index] = num1;
41+
if nums1_i == 0 { nums1_done = true; }
42+
else { nums1_i -= 1; }
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::Solution;
54+
55+
#[test]
56+
fn example_1() {
57+
let mut nums1 = vec![1,2,3,0,0,0];
58+
let m = 3;
59+
let mut nums2 = vec![2,5,6];
60+
let n = 3;
61+
Solution::merge(&mut nums1, m, &mut nums2, n);
62+
assert_eq!(nums1, vec![1,2,2,3,5,6]);
63+
}
64+
65+
#[test]
66+
fn example_2() {
67+
let mut nums1 = vec![1];
68+
let m = 1;
69+
let mut nums2 = vec![];
70+
let n = 0;
71+
Solution::merge(&mut nums1, m, &mut nums2, n);
72+
assert_eq!(nums1, vec![1]);
73+
}
74+
75+
}

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)