diff --git a/src/bin/lc-373.rs b/src/bin/lc-373.rs new file mode 100644 index 0000000..c76032d --- /dev/null +++ b/src/bin/lc-373.rs @@ -0,0 +1,34 @@ +use std::cmp::Ordering; + +struct Solution; + +impl Solution { + pub fn k_smallest_pairs(nums1: Vec, nums2: Vec, k: i32) -> Vec> { + use std::collections::{BinaryHeap,HashSet}; + let mut pq = BinaryHeap::<(i32,usize,usize)>::new(); + pq.push((-nums1[0]-nums2[0],0,0)); + let m=nums1.len(); + let n = nums2.len(); + let mut vis = HashSet::<(usize,usize)>::new(); + let mut res = Vec::new(); + let mut cnt = 0; + while let Some((_,i,j)) = pq.pop() { + if vis.contains(&(i,j)) {continue;} + vis.insert((i,j)); + res.push(vec![nums1[i],nums2[j]]); + cnt += 1; + if cnt == k as usize {break;} + if i+1!=m { + pq.push((-nums1[i+1]-nums2[j],i+1,j)); + } + if j+1!=n { + pq.push((-nums1[i]-nums2[j+1],i,j+1)); + } + } + res + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/bin/lc-402.rs b/src/bin/lc-402.rs new file mode 100644 index 0000000..f5b971a --- /dev/null +++ b/src/bin/lc-402.rs @@ -0,0 +1,34 @@ +use std::collections::VecDeque; + +struct Solution; + +impl Solution { + pub fn remove_kdigits(num: String, k: i32) -> String { + use std::collections::VecDeque;; + let num = num.into_bytes(); // TODO + let mut st = VecDeque::::new(); + let mut cnt = 0; + for x in num { + while let Some(&y) = st.back() { + if x >= y || cnt == k { break; } + st.pop_back(); + cnt += 1; + } + st.push_back(x); + } + for _ in cnt..k { + st.pop_back(); + } + while let Some(48) = st.front() { + st.pop_front(); + } + if st.is_empty() { st.push_back(48); } + String::from_utf8(st.into_iter().collect::>()).unwrap() + } +} + +fn main() { + let num = "1432219".to_string(); + let k = 3; + println!("{}",Solution::remove_kdigits(num,k)); +} \ No newline at end of file diff --git a/src/bin/lc-406.rs b/src/bin/lc-406.rs new file mode 100644 index 0000000..78e52ef --- /dev/null +++ b/src/bin/lc-406.rs @@ -0,0 +1,23 @@ +struct Solution; + +impl Solution { + pub fn reconstruct_queue(mut people: Vec>) -> Vec> { + people.sort_unstable_by(|x,y|{ + if x[0] == y[0] { + x[1].cmp(&y[1]) + } else { + y[0].cmp(&x[0]) + } + }); + let n = people.len(); + let mut res = Vec::>::with_capacity(n); + for curr in people { + res.insert(curr[1] as usize, curr); + } + res + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/bin/lc-409.rs b/src/bin/lc-409.rs new file mode 100644 index 0000000..ef0f2f5 --- /dev/null +++ b/src/bin/lc-409.rs @@ -0,0 +1,27 @@ +struct Solution; + +impl Solution { + pub fn longest_palindrome(s: String) -> i32 { + use std::collections::HashMap; + let mut s = s.into_bytes(); + let mut mp = HashMap::::new(); + for i in s { + if let Some(&v) = mp.get(&i) { + mp.insert(i,v+1); + }else{ + mp.insert(i,1); + } + } + let mut res = 0; + let mut f = 0; + for (_,v) in mp { + if v & 1 != 0 { f = 1; } + res += v >> 1; + } + res + f + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 41cd7a3..ad8afc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,10 @@ -trait Printable { - fn print(a:T); -} - -struct Test; - -impl Printable for Test where T: std::fmt::Display { - fn print(a:T) { - println!("{a}"); - } -} - +mod test_mod; +mod test_mods; +mod a_sibling_file; fn main() { - ::print(1.5); -} + use test_mod::a_mod::hello_world; + use test_mods::another_mod; + hello_world(); + another_mod::another_fn(); + a_sibling_file::sibling_mod::sibling_fn(); +} \ No newline at end of file