mirror of
https://github.com/Xevion/leetcode.git
synced 2025-12-06 15:15:29 -06:00
Move all solutions into separate directory (cleanup)
the beginning
This commit is contained in:
7
solutions/median-of-two-sorted-arrays/Cargo.lock
generated
Normal file
7
solutions/median-of-two-sorted-arrays/Cargo.lock
generated
Normal file
@@ -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"
|
||||
8
solutions/median-of-two-sorted-arrays/Cargo.toml
Normal file
8
solutions/median-of-two-sorted-arrays/Cargo.toml
Normal file
@@ -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]
|
||||
31
solutions/median-of-two-sorted-arrays/README.md
Normal file
31
solutions/median-of-two-sorted-arrays/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# median-of-two-sorted-arrays
|
||||
|
||||
## Two Pointers
|
||||
|
||||
When building a merge sort algorithm, the last step is to merge two sorted arrays - just like with this problem (at least for the brute force soluton).
|
||||
One interesting thing about this is that we can keep track of how far into the final sorted array we are during this 'merge' step.
|
||||
|
||||
For example, merging `[1, 2, 3]` and `[2, 7, 9]`, we can determine which index of the final array we are at.
|
||||
|
||||
```
|
||||
[1] => 1
|
||||
[1, 2] => 2
|
||||
[1, 2, 2] => 3
|
||||
[1, 2, 2, 3] => 4
|
||||
[1, 2, 2, 3, 7] => 5
|
||||
[1, 2, 2, 3, 7, 9] => 6
|
||||
```
|
||||
|
||||
Given this, we can also determine at what point we have 'reached' the middle of the final array.
|
||||
Given this, we can extract the middle value(s) from the final array, iterating just like we do during a merge sort, but only until we reach the middle.
|
||||
|
||||
In the example above, we reeach the middle at index 3 - and given half the final array's sie is 3 - that's the index we want to stop at.
|
||||
|
||||
The actual implementation is with two pointers - the index we're currently iterating over in each array.
|
||||
We keep track of the last two values we've seen, and when we reach the middle, we can determine if we need to return one or two values.
|
||||
|
||||
> For a real implementation, you could setup two different algorithms, one for even-size final arrays, and one for odd. This would remove a couple instructions fo the loop, but given that the Big O is the same - it's not worth it for Leetcode.
|
||||
|
||||
## Binary Search
|
||||
|
||||
TODO: Writeup
|
||||
@@ -0,0 +1,44 @@
|
||||
struct Solution {}
|
||||
|
||||
impl Solution {
|
||||
pub fn find_median_sorted_arrays(a: Vec<i32>, b: Vec<i32>) -> f64 {
|
||||
let (a_length, b_length) = (a.len(), b.len());
|
||||
let (mut a_pointer, mut b_pointer, mut current, mut previous) = (0, 0, 0, 0);
|
||||
|
||||
for _ in 0..=((a_length+b_length)/2) {
|
||||
previous = current;
|
||||
|
||||
if a_pointer != a_length && b_pointer != b_length {
|
||||
// Main merge point (as long as both arrays have space to iterate left)
|
||||
if a[a_pointer] > b[b_pointer] {
|
||||
current = b[b_pointer];
|
||||
b_pointer += 1;
|
||||
} else {
|
||||
current = a[a_pointer];
|
||||
a_pointer += 1;
|
||||
}
|
||||
} else if a_pointer < a_length {
|
||||
// Other array has no more space to iterate
|
||||
current = a[a_pointer];
|
||||
a_pointer += 1;
|
||||
} else {
|
||||
// Other array has no more space to iterate
|
||||
current = b[b_pointer];
|
||||
b_pointer += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (a_length + b_length) % 2 == 1 {
|
||||
current as f64
|
||||
} else {
|
||||
(current + previous) as f64 / 2.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"{}",
|
||||
Solution::find_median_sorted_arrays(vec![1, 3], vec![2])
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user