forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update 0004-median-of-two-sorted-arrays.rs
- Loading branch information
1 parent
ac77c60
commit 582ca91
Showing
1 changed file
with
7 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,12 @@ | ||
impl Solution { | ||
pub fn find_median_sorted_arrays(mut nums1: Vec<i32>, mut nums2: Vec<i32>) -> f64 { | ||
let total = nums1.len() + nums2.len(); | ||
let half = total / 2; | ||
|
||
if nums1.len() > nums2.len() { | ||
std::mem::swap(&mut nums1, &mut nums2); | ||
pub fn find_median_sorted_arrays(mut nums1: Vec<i32>, nums2: Vec<i32>) -> f64 { | ||
for val in nums2 { | ||
nums1.insert(nums1.binary_search(&val).unwrap_or_else(|e| e), val); | ||
} | ||
|
||
let mut left = 0; | ||
let mut right = nums1.len(); | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2; | ||
let pointer = half - mid; | ||
|
||
let base_left = if mid > 0 { | ||
nums1[mid - 1] as f64 | ||
} else { | ||
f64::MIN | ||
}; | ||
|
||
let base_right = if mid < nums1.len() { | ||
nums1[mid] as f64 | ||
} else { | ||
f64::MAX | ||
}; | ||
|
||
let ref_left = if pointer > 0 { | ||
nums2[pointer - 1] as f64 | ||
} else { | ||
f64::MIN | ||
}; | ||
|
||
let ref_right = if pointer < nums2.len() { | ||
nums2[pointer] as f64 | ||
} else { | ||
f64::MAX | ||
}; | ||
|
||
if base_left <= ref_right && ref_left <= base_right { | ||
if total % 2 == 1 { | ||
return base_right.min(ref_right); | ||
} else { | ||
return (base_left.max(ref_left) + base_right.min(ref_right)) / 2.0; | ||
} | ||
} else if base_left > ref_right { | ||
right = mid - 1; | ||
} else { | ||
left = mid + 1; | ||
} | ||
if nums1.len()%2==0 { | ||
(nums1[(nums1.len()-1) / 2] + nums1[nums1.len() / 2]) as f64 / 2.0 | ||
} else { | ||
nums1[(nums1.len()-1) / 2] as f64 | ||
} | ||
|
||
panic!("Arrays are not sorted"); | ||
} | ||
} |