Skip to content

Commit e53f526

Browse files
committed
Add 004 solution
1 parent 85ff4d2 commit e53f526

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use std::num;
2+
3+
/**
4+
* [004] median of two sorted arrays
5+
*
6+
* 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
7+
*
8+
* 算法的时间复杂度应该为 O(log (m+n)) 。
9+
*
10+
*
11+
* 示例 1:
12+
*
13+
* 输入:nums1 = [1,3], nums2 = [2]
14+
* 输出:2.00000
15+
* 解释:合并数组 = [1,2,3] ,中位数 2
16+
* 示例 2:
17+
*
18+
* 输入:nums1 = [1,2], nums2 = [3,4]
19+
* 输出:2.50000
20+
* 解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5
21+
*
22+
* 提示:
23+
*
24+
* nums1.length == m
25+
* nums2.length == n
26+
* 0 <= m <= 1000
27+
* 0 <= n <= 1000
28+
* 1 <= m + n <= 2000
29+
* -106 <= nums1[i], nums2[i] <= 106
30+
*/
31+
use std::cmp;
32+
33+
struct Solution;
34+
35+
/**
36+
* 思路:
37+
* 1. 归并排序,取中位数,时间复杂度 O(m+n),空间复杂度 O(m+n)
38+
* 2. 找到两个数组的中位数的位置,时间复杂度 O(m+n),空间复杂度 O(1)
39+
* 3. 二分法找到两个数组的中位数的位置,时间复杂度 O(log(m+n)),空间复杂度 O(1)
40+
* 4. 中位数的统计学意义,将两个数组拆分,时间复杂度 O(log(min(m+n))),空间复杂度 O(1)
41+
*/
42+
impl Solution {
43+
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
44+
let length_1 = nums1.len();
45+
let length_2 = nums2.len();
46+
let total_length = length_1 + length_2;
47+
if (total_length % 2 == 1) {
48+
let medium = total_length / 2;
49+
Solution::find_median_sorted_arrays_by_binary_search(&nums1, &nums2, medium + 1) as f64
50+
} else {
51+
let medium = total_length / 2;
52+
let left = medium - 1;
53+
let right = medium;
54+
((Solution::find_median_sorted_arrays_by_binary_search(&nums1, &nums2, left + 1)
55+
+ Solution::find_median_sorted_arrays_by_binary_search(&nums1, &nums2, right + 1))
56+
as f64)
57+
/ 2.0
58+
}
59+
}
60+
61+
fn find_median_sorted_arrays_by_binary_search(nums1: &[i32], nums2: &[i32], k: usize) -> i32 {
62+
let length1 = nums1.len();
63+
let length2 = nums2.len();
64+
let mut index1 = 0;
65+
let mut index2 = 0;
66+
let mut k = k;
67+
let mut new_index1 = 0;
68+
let mut new_index2 = 0;
69+
loop {
70+
println!(
71+
"index1:{} index2:{} newIndex1:{} newIndex2:{}, k:{}",
72+
index1, index2, new_index1, new_index2, k
73+
);
74+
if (index1 == nums1.len()) {
75+
return nums2[index2 + k - 1];
76+
}
77+
if (index2 == nums2.len()) {
78+
return nums1[index1 + k - 1];
79+
}
80+
if (k == 1) {
81+
return cmp::min(nums1[index1], nums2[index2]);
82+
}
83+
let half = k / 2;
84+
new_index1 = cmp::min(index1 + half, length1) - 1;
85+
new_index2 = cmp::min(index2 + half, length2) - 1;
86+
if (nums1[new_index1] <= nums2[new_index2]) {
87+
k -= new_index1 - index1 + 1;
88+
index1 = new_index1 + 1;
89+
} else {
90+
k -= new_index2 - index2 + 1;
91+
index2 = new_index2 + 1;
92+
}
93+
}
94+
}
95+
}
96+
97+
#[cfg(test)]
98+
mod tests {
99+
use super::*;
100+
101+
#[test]
102+
fn test_004() {
103+
assert_eq!(
104+
Solution::find_median_sorted_arrays(vec![1, 3], vec![2]),
105+
2.0
106+
);
107+
108+
assert_eq!(
109+
Solution::find_median_sorted_arrays(vec![1, 2], vec![3, 4]),
110+
2.5
111+
)
112+
}
113+
}

0 commit comments

Comments
 (0)