summaryrefslogtreecommitdiff
path: root/median-of-two-sorted-arrays
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2024-06-27 11:30:16 +0200
committerOrangerot <purple@orangerot.dev>2024-06-27 11:30:16 +0200
commit4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch)
tree0072cca328fe5adb2ed61004010228ff85e2164d /median-of-two-sorted-arrays
Initial commitHEADmain
Diffstat (limited to 'median-of-two-sorted-arrays')
-rw-r--r--median-of-two-sorted-arrays/Cargo.toml8
-rw-r--r--median-of-two-sorted-arrays/src/main.rs23
2 files changed, 31 insertions, 0 deletions
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..a8557aa
--- /dev/null
+++ b/median-of-two-sorted-arrays/src/main.rs
@@ -0,0 +1,23 @@
+fn main() {
+ println!("Hello, world!");
+ let tests = vec![
+ (vec![1,3], vec![2], 2.0),
+ (vec![1,2], vec![3,4], 2.5)
+ ];
+ for test in tests {
+ println!("{:?}{:?} is {} should be {}", test.0, test.1,
+ Solution::find_median_sorted_arrays(test.0.clone(), test.1.clone()), test.2);
+ }
+}
+
+struct Solution {}
+impl Solution {
+ pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
+ let mut a = nums1.clone();
+ a.append(&mut nums2.clone());
+ a.sort();
+ let median = a.get((a.len() / 2)-1..((a.len() + 1)/2)).unwrap();
+ println!("{:?}", median);
+ (median.iter().sum::<i32>() as f64) / (median.len() as f64)
+ }
+}