Skip to content

Commit

Permalink
solve aylei#4
Browse files Browse the repository at this point in the history
  • Loading branch information
Cupnfish authored Jul 31, 2019
1 parent 0e10183 commit d3a187b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/n0004_median_of_two_sorted_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,27 @@ pub struct Solution {}
// submission codes start here

// TODO: nth slice
use std::rc::Rc;
impl Solution {
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
1.0
let (lhs,rhs) = if nums1.len() >nums2.len(){(Rc::new(nums2),Rc::new(nums1))}else{(Rc::new(nums1),Rc::new(nums2))};
let (n,m) = (Rc::clone(&lhs).len(),Rc::clone(&rhs).len());
let (mut lmax1,mut rmin1,mut lmax2,mut rmin2) = (0,0,0,0);
let (mut lo,mut hi) = (0,n*2);
while lo <= hi{
let c1 = (lo+hi)/2;
let c2 = m+n-c1;
lmax1 = if c1==0{i32::min_value()}else{Rc::clone(&lhs)[(c1-1)/2]};
rmin1 = if c1==2*n{i32::max_value()}else{Rc::clone(&lhs)[c1/2]};
lmax2 = if c2==0{i32::min_value()}else{Rc::clone(&rhs)[(c2-1)/2]};
rmin2 = if c2==2*m{i32::max_value()}else{Rc::clone(&rhs)[c2/2]};

if lmax1>rmin2{hi=c1-1}
else if lmax2>rmin1{lo = c1+1}
else{break}
}
(std::cmp::max(lmax1, lmax2) + std::cmp::min(rmin1, rmin2))as f64 / 2.0
}
}

// submission codes end
Expand Down

0 comments on commit d3a187b

Please sign in to comment.