From e57953fd8c2b8642ebbae7f7b8c87797ef10c432 Mon Sep 17 00:00:00 2001 From: erius Date: Fri, 3 May 2024 12:06:51 +0300 Subject: [PATCH] Implemented p22, TODO - implement p23 --- src/lib.rs | 1 + src/p22_generate_parentheses.rs | 14 +++++++++++--- src/p23_merge_k_sorted_lists.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/p23_merge_k_sorted_lists.rs diff --git a/src/lib.rs b/src/lib.rs index 00b3c4f..4215b19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,3 +22,4 @@ pub mod p19_remove_nth_node_from_end_of_list; pub mod p20_valid_parentheses; pub mod p21_merge_two_sorted_lists; pub mod p22_generate_parentheses; +pub mod p23_merge_k_sorted_lists; diff --git a/src/p22_generate_parentheses.rs b/src/p22_generate_parentheses.rs index b3e6968..2918f4a 100644 --- a/src/p22_generate_parentheses.rs +++ b/src/p22_generate_parentheses.rs @@ -1,9 +1,17 @@ pub struct Solution; impl Solution { - // TODO: implement pub fn generate_parenthesis(n: i32) -> Vec { - - return vec![]; + let mut operations = vec![(0, 0, "".to_string())]; + let mut result = vec![]; + while let Some((left, right, s)) = operations.pop() { + if s.len() == n as usize * 2 { + result.push(s); + continue; + } + if left < n { operations.push((left + 1, right, s.clone() + "(")); } + if right < left { operations.push((left, right + 1, s + ")")); } + } + return result; } } diff --git a/src/p23_merge_k_sorted_lists.rs b/src/p23_merge_k_sorted_lists.rs new file mode 100644 index 0000000..9a82e0f --- /dev/null +++ b/src/p23_merge_k_sorted_lists.rs @@ -0,0 +1,27 @@ +use super::linked_list::ListNode; + +pub struct Solution; +impl Solution { + // TODO: implement + pub fn merge_k_lists(lists: Vec>>) -> Option> { + + return None; + } +} + +#[test] +fn test1() { + assert_eq!(Solution::merge_k_lists(vec![ + list![1,4,5], list![1,3,4], list![2,6] + ]), list![1,1,2,3,4,4,5,6]); +} + +#[test] +fn test2() { + assert_eq!(Solution::merge_k_lists(vec![]), list![]); +} + +#[test] +fn test3() { + assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]); +}