Skip to content

Commit 7bca15a

Browse files
authored
Update 4.median-of-two-sorted-array.md
1 parent aabcdf6 commit 7bca15a

File tree

1 file changed

+89
-65
lines changed

1 file changed

+89
-65
lines changed

problems/4.median-of-two-sorted-array.md

Lines changed: 89 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
## 题目地址
1+
## 题目地址(4. 寻找两个正序数组的中位数)
22
https://leetcode.com/problems/median-of-two-sorted-arrays/
33

44
## 题目描述
55
```
6-
There are two sorted arrays nums1 and nums2 of size m and n respectively.
6+
给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2
77
8-
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
8+
请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))
99
10-
You may assume nums1 and nums2 cannot be both empty.
10+
你可以假设 nums1 和 nums2 不会同时为空。
1111
12-
Example 1:
12+
 
13+
14+
示例 1:
1315
1416
nums1 = [1, 3]
1517
nums2 = [2]
1618
17-
The median is 2.0
18-
Example 2:
19+
则中位数是 2.0
20+
示例 2:
1921
2022
nums1 = [1, 2]
2123
nums2 = [3, 4]
2224
23-
The median is (2 + 3)/2 = 2.5
25+
则中位数是 (2 + 3)/2 = 2.5
26+
2427
```
2528

2629
## 思路
2730
首先了解一下Median的概念,一个数组中median就是把数组分成左右等分的中位数。
2831

2932
如下图:
30-
![median](../assets/problems/4.median-of-two-sorted-array-1.jpg)
33+
![image.png](https://pic.leetcode-cn.com/100b34b378d0667969e7a4ca537c74e5103ce302731796740b3fa62b8bc55629-image.png)
34+
3135

3236
这道题,很容易想到暴力解法,时间复杂度和空间复杂度都是`O(m+n)`, 不符合题中给出`O(log(m+n))`时间复杂度的要求。
3337
我们可以从简单的解法入手,试了一下,暴力解法也是可以被Leetcode Accept的. 分析中会给出两种解法,暴力求解和二分解法。
@@ -44,7 +48,8 @@ The median is (2 + 3)/2 = 2.5
4448
5. 如果`i`移动到`A`数组最后,那么直接把剩下的所有`B`依次放入新的数组中.
4549

4650
Merge的过程如下图。
47-
![merge two sorted array](../assets/problems/4.median-of-two-sorted-array-2.jpg)
51+
![image.png](https://pic.leetcode-cn.com/966c9a0fea7a5f433b82d660f82c5d8184a2ac73b8362d7be435aa0f63377a4c-image.png)
52+
4853

4954

5055
*时间复杂度: `O(m+n) - m is length of A, n is length of B`*
@@ -60,13 +65,14 @@ Merge的过程如下图。
6065
对数组A的做partition的位置是区间`[0,m]`
6166

6267
如图:
63-
![partition A,B](../assets/problems/4.median-of-two-sorted-array-3.png)
68+
![image.png](https://pic.leetcode-cn.com/816717da264c9e6970bfe0d696d9076febfe04b819f80a224cf2c73845d0f161-image.png)
6469

6570
下图给出几种不同情况的例子(注意但左边或者右边没有元素的时候,左边用`INF_MIN`,右边用`INF_MAX`表示左右的元素:
66-
![median examples](../assets/problems/4.median-of-two-sorted-array-5.png)
71+
![image.png](https://pic.leetcode-cn.com/fe03bdcf4db4d74a0bebaa2907df3038c080e5915932fd581d06d4eb930b3035-image.png)
6772

6873
下图给出具体做的partition 解题的例子步骤,
69-
![median partition example](../assets/problems/4.median-of-two-sorted-array-4.png)
74+
![image.png](https://pic.leetcode-cn.com/1c2093328c4edf06e416d0f43a94ed42b5a46ecc9f7ed72004b40b9fb47e12a4-image.png)
75+
7076

7177
*时间复杂度: `O(log(min(m, n)) - m is length of A, n is length of B`*
7278

@@ -88,9 +94,17 @@ median = max(maxLeftA, maxLeftB)
8894
median = (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2
8995
```
9096

91-
## 代码(Java code)
97+
## 代码
98+
99+
100+
代码支持: Java,JS:
101+
102+
103+
Java Code:
104+
92105
*解法一 - 暴力解法(Brute force)*
93-
```java
106+
107+
```java []
94108
class MedianTwoSortedArrayBruteForce {
95109
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
96110
int[] newArr = mergeTwoSortedArray(nums1, nums2);
@@ -127,54 +141,7 @@ class MedianTwoSortedArrayBruteForce {
127141
}
128142
}
129143
```
130-
*解法二 - 二分查找(Binary Search)*
131-
```java
132-
class MedianSortedTwoArrayBinarySearch {
133-
public static double findMedianSortedArraysBinarySearch(int[] nums1, int[] nums2) {
134-
// do binary search for shorter length array, make sure time complexity log(min(m,n)).
135-
if (nums1.length > nums2.length) {
136-
return findMedianSortedArraysBinarySearch(nums2, nums1);
137-
}
138-
int m = nums1.length;
139-
int n = nums2.length;
140-
int lo = 0;
141-
int hi = m;
142-
while (lo <= hi) {
143-
// partition A position i
144-
int i = lo + (hi - lo) / 2;
145-
// partition B position j
146-
int j = (m + n + 1) / 2 - i;
147-
148-
int maxLeftA = i == 0 ? Integer.MIN_VALUE : nums1[i - 1];
149-
int minRightA = i == m ? Integer.MAX_VALUE : nums1[i];
150-
151-
int maxLeftB = j == 0 ? Integer.MIN_VALUE : nums2[j - 1];
152-
int minRightB = j == n ? Integer.MAX_VALUE : nums2[j];
153-
154-
if (maxLeftA <= minRightB && maxLeftB <= minRightA) {
155-
// total length is even
156-
if ((m + n) % 2 == 0) {
157-
return (double) (Math.max(maxLeftA, maxLeftB) + Math.min(minRightA, minRightB)) / 2;
158-
} else {
159-
// total length is odd
160-
return (double) Math.max(maxLeftA, maxLeftB);
161-
}
162-
} else if (maxLeftA > minRightB) {
163-
// binary search left half
164-
hi = i - 1;
165-
} else {
166-
// binary search right half
167-
lo = i + 1;
168-
}
169-
}
170-
return 0.0;
171-
}
172-
}
173-
```
174-
175-
## 代码 (javascript code)
176-
*解法一 - 暴力解法(Brute force)*
177-
```js
144+
```javascript []
178145
/**
179146
* @param {number[]} nums1
180147
* @param {number[]} nums2
@@ -206,8 +173,12 @@ var findMedianSortedArrays = function(nums1, nums2) {
206173
};
207174
```
208175

176+
***复杂度分析***
177+
- 时间复杂度:$O(max(m, n))$
178+
- 空间复杂度:$O(m + n)$
179+
209180
*解法二 - 二分查找(Binary Search)*
210-
```js
181+
```js []
211182
/**
212183
* 二分解法
213184
* @param {number[]} nums1
@@ -243,4 +214,57 @@ var findMedianSortedArrays = function(nums1, nums2) {
243214
}
244215
}
245216
};
246-
```
217+
```
218+
```java []
219+
class MedianSortedTwoArrayBinarySearch {
220+
public static double findMedianSortedArraysBinarySearch(int[] nums1, int[] nums2) {
221+
// do binary search for shorter length array, make sure time complexity log(min(m,n)).
222+
if (nums1.length > nums2.length) {
223+
return findMedianSortedArraysBinarySearch(nums2, nums1);
224+
}
225+
int m = nums1.length;
226+
int n = nums2.length;
227+
int lo = 0;
228+
int hi = m;
229+
while (lo <= hi) {
230+
// partition A position i
231+
int i = lo + (hi - lo) / 2;
232+
// partition B position j
233+
int j = (m + n + 1) / 2 - i;
234+
235+
int maxLeftA = i == 0 ? Integer.MIN_VALUE : nums1[i - 1];
236+
int minRightA = i == m ? Integer.MAX_VALUE : nums1[i];
237+
238+
int maxLeftB = j == 0 ? Integer.MIN_VALUE : nums2[j - 1];
239+
int minRightB = j == n ? Integer.MAX_VALUE : nums2[j];
240+
241+
if (maxLeftA <= minRightB && maxLeftB <= minRightA) {
242+
// total length is even
243+
if ((m + n) % 2 == 0) {
244+
return (double) (Math.max(maxLeftA, maxLeftB) + Math.min(minRightA, minRightB)) / 2;
245+
} else {
246+
// total length is odd
247+
return (double) Math.max(maxLeftA, maxLeftB);
248+
}
249+
} else if (maxLeftA > minRightB) {
250+
// binary search left half
251+
hi = i - 1;
252+
} else {
253+
// binary search right half
254+
lo = i + 1;
255+
}
256+
}
257+
return 0.0;
258+
}
259+
}
260+
```
261+
***复杂度分析***
262+
- 时间复杂度:$O(log(min(m, n)))$
263+
- 空间复杂度:$O(log(min(m, n)))$
264+
265+
266+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
267+
268+
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
269+
270+
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)

0 commit comments

Comments
 (0)