Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d3a187b

Browse files
solve aylei#4
1 parent 0e10183 commit d3a187b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

‎src/n0004_median_of_two_sorted_arrays.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,27 @@ pub struct Solution {}
3131
// submission codes start here
3232

3333
// TODO: nth slice
34+
use std::rc::Rc;
3435
impl Solution {
3536
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
36-
1.0
37+
let (lhs,rhs) = if nums1.len() >nums2.len(){(Rc::new(nums2),Rc::new(nums1))}else{(Rc::new(nums1),Rc::new(nums2))};
38+
let (n,m) = (Rc::clone(&lhs).len(),Rc::clone(&rhs).len());
39+
let (mut lmax1,mut rmin1,mut lmax2,mut rmin2) = (0,0,0,0);
40+
let (mut lo,mut hi) = (0,n*2);
41+
while lo <= hi{
42+
let c1 = (lo+hi)/2;
43+
let c2 = m+n-c1;
44+
lmax1 = if c1==0{i32::min_value()}else{Rc::clone(&lhs)[(c1-1)/2]};
45+
rmin1 = if c1==2*n{i32::max_value()}else{Rc::clone(&lhs)[c1/2]};
46+
lmax2 = if c2==0{i32::min_value()}else{Rc::clone(&rhs)[(c2-1)/2]};
47+
rmin2 = if c2==2*m{i32::max_value()}else{Rc::clone(&rhs)[c2/2]};
48+
49+
if lmax1>rmin2{hi=c1-1}
50+
else if lmax2>rmin1{lo = c1+1}
51+
else{break}
3752
}
53+
(std::cmp::max(lmax1, lmax2) + std::cmp::min(rmin1, rmin2))as f64 / 2.0
54+
}
3855
}
3956

4057
// submission codes end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /