From 0ac04eb13427f3de641a6e8e1251c41b621d989d Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 2 Oct 2023 19:50:29 -0500 Subject: [PATCH] Two Pointer attempt for median-of-two-sorted-arrays --- .gitignore | 3 +- median-of-two-sorted-arrays/Cargo.lock | 7 ++ median-of-two-sorted-arrays/Cargo.toml | 8 ++ median-of-two-sorted-arrays/src/main.rs | 102 ++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 median-of-two-sorted-arrays/Cargo.lock create mode 100644 median-of-two-sorted-arrays/Cargo.toml create mode 100644 median-of-two-sorted-arrays/src/main.rs diff --git a/.gitignore b/.gitignore index 75ec3f0..b31aa23 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode/* \ No newline at end of file +.vscode/* +**/target \ No newline at end of file diff --git a/median-of-two-sorted-arrays/Cargo.lock b/median-of-two-sorted-arrays/Cargo.lock new file mode 100644 index 0000000..35b9032 --- /dev/null +++ b/median-of-two-sorted-arrays/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "median-of-two-sorted-arrays" +version = "0.1.0" diff --git a/median-of-two-sorted-arrays/Cargo.toml b/median-of-two-sorted-arrays/Cargo.toml new file mode 100644 index 0000000..58b9f7b --- /dev/null +++ b/median-of-two-sorted-arrays/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "median-of-two-sorted-arrays" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/median-of-two-sorted-arrays/src/main.rs b/median-of-two-sorted-arrays/src/main.rs new file mode 100644 index 0000000..9778786 --- /dev/null +++ b/median-of-two-sorted-arrays/src/main.rs @@ -0,0 +1,102 @@ +struct Solution {} + +impl Solution { + pub fn find_median(v: Vec) -> f64 { + let middle = (v.len() / 2) - 1; + if v.len() % 2 == 1 { + return v[middle] as f64; + } + + (v[middle] + v[middle + 1]) as f64 / 2.0 + } + + pub fn find_median_sorted_arrays(a: Vec, b: Vec) -> f64 { + let mut i = 0; + let mut j = 0; + let mut current = 0; + let mut previous = 0; + let mut count = 0; + let is_average = (a.len() + b.len()) % 2 == 0; + let target = (a.len() + b.len()) / 2; + + if target == 0 { + if a.len() == 0 && b.len() == 0 { + return 0.0; + } else if a.len() == 0 { + return b[0] as f64; + } else if b.len() == 0 { + return a[0] as f64; + } + } + + if a.len() == 0 { + return Solution::find_median(b); + } else if b.len() == 0 { + return Solution::find_median(a); + } + + println!("current={current} prev={previous} i={i} j={j}"); + while count <= target { + previous = current; + + if i < a.len() && a[i] <= b[j] { + current = a[i]; + i += 1; + } else { + current = b[j]; + j += 1; + } + println!("current={current} prev={previous} i={i} j={j}"); + + // Case in which array A or B is composed entirely of smaller numbers (i.e. [1, 2] and [3, 4]) - join the last and first elements. + if i == target { + if is_average { + previous = current; + current = b[0]; + } + break; + } else if j == target { + if is_average { + previous = current; + current = a[0]; + } + break; + } + + count += 1; + } + + if is_average { + (current + previous) as f64 / 2.0 + } else { + current as f64 + } + } +} + +fn main() { + // assert_eq!( + // Solution::find_median_sorted_arrays(vec![1, 3], vec![2]), + // 2.0 + // ); + + // assert_eq!( + // Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]), + // 2.5 + // ); + + // println!( + // "{}", + // Solution::find_median_sorted_arrays(vec![0, 0, 0, 0, 0], vec![-1, 0, 0, 0, 0, 0, 1]) + // ); + + println!( + "{}", + Solution::find_median_sorted_arrays(vec![1, 3], vec![2]) + ); + + // println!( + // "{}", + // Solution::find_median_sorted_arrays(vec![3], vec![-1, -2]) + // ); +}