|
| 1 | +/** |
| 2 | + * [4] Median of Two Sorted Arrays |
| 3 | + * |
| 4 | + * Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. |
| 5 | + * The overall run time complexity should be O(log (m+n)). |
| 6 | + * |
| 7 | + * <strong class="example">Example 1: |
| 8 | + * |
| 9 | + * Input: nums1 = [1,3], nums2 = [2] |
| 10 | + * Output: 2.00000 |
| 11 | + * Explanation: merged array = [1,2,3] and median is 2. |
| 12 | + * |
| 13 | + * <strong class="example">Example 2: |
| 14 | + * |
| 15 | + * Input: nums1 = [1,2], nums2 = [3,4] |
| 16 | + * Output: 2.50000 |
| 17 | + * Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. |
| 18 | + * |
| 19 | + * |
| 20 | + * Constraints: |
| 21 | + * |
| 22 | + * nums1.length == m |
| 23 | + * nums2.length == n |
| 24 | + * 0 <= m <= 1000 |
| 25 | + * 0 <= n <= 1000 |
| 26 | + * 1 <= m + n <= 2000 |
| 27 | + * -10^6 <= nums1[i], nums2[i] <= 10^6 |
| 28 | + * |
| 29 | + */ |
| 30 | +pub struct Solution {} |
| 31 | + |
| 32 | +// problem: https://leetcode.com/problems/median-of-two-sorted-arrays/ |
| 33 | +// discuss: https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/?currentPage=1&orderBy=most_votes&query= |
| 34 | + |
| 35 | +// submission codes start here |
| 36 | + |
| 37 | +impl Solution { |
| 38 | + pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 { |
| 39 | + use std::cmp::min; |
| 40 | + |
| 41 | + fn get_kth_element(nums1: &Vec<i32>, nums2: &Vec<i32>, k: usize) -> i32 { |
| 42 | + let mut start1 = 0; |
| 43 | + let mut start2 = 0; |
| 44 | + let mut k = k; |
| 45 | + loop { |
| 46 | + if start1 == nums1.len() { |
| 47 | + return nums2[start2 + k - 1]; |
| 48 | + } |
| 49 | + if start2 == nums2.len() { |
| 50 | + return nums1[start1 + k - 1]; |
| 51 | + } |
| 52 | + if k == 1 { |
| 53 | + return min(nums1[start1], nums2[start2]); |
| 54 | + } |
| 55 | + let step = k / 2; |
| 56 | + let next_start1 = min(start1 + step, nums1.len()) - 1; |
| 57 | + let next_start2 = min(start2 + step, nums2.len()) - 1; |
| 58 | + if nums1[next_start1] <= nums2[next_start2] { |
| 59 | + k -= next_start1 - start1 + 1; |
| 60 | + start1 = next_start1 + 1; |
| 61 | + } else { |
| 62 | + k -= next_start2 - start2 + 1; |
| 63 | + start2 = next_start2 + 1; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + let len = nums1.len() + nums2.len(); |
| 68 | + let k = (len + 1) / 2; |
| 69 | + if len % 2 == 0 { |
| 70 | + ((get_kth_element(&nums1, &nums2, k) + get_kth_element(&nums1, &nums2, k + 1)) as f64) |
| 71 | + / 2.0 |
| 72 | + } else { |
| 73 | + get_kth_element(&nums1, &nums2, k) as f64 |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// submission codes end |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::*; |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_4() { |
| 86 | + assert_eq!( |
| 87 | + Solution::find_median_sorted_arrays(vec![1, 3], vec![2]), |
| 88 | + 2.0f64 |
| 89 | + ); |
| 90 | + assert_eq!( |
| 91 | + Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]), |
| 92 | + 2.5f64 |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments