两个排序数组的中位数

中英题面

  给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。

  There are two sorted arrays nums1 and nums2 of size m and n respectively.

  请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

  Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

  示例 1:

  nums1 = [1, 3]
  nums2 = [2]

  中位数是 2.0

  Example 1:

  nums1 = [1, 3]
  nums2 = [2]

  The median is 2.0

 示例 2:

  nums1 = [1, 2]
  nums2 = [3, 4]

  中位数是 (2 + 3)/2 = 2.5

  Example 2:

  nums1 = [1, 2]
  nums2 = [3, 4]

  The median is (2 + 3)/2 = 2.5

算法

  将原题转化为在两个有序数列中查找第k小的元素。

  对于长度为m和n的两个有序数列a和b,考虑0 < i < m和0 < j < n且i + j + 2 == k。

  若a[i] < b[j],则a[0..i]与b[j..(n – 1)]中比无所要查找的元素,将其删去后更新k值递归处理。

  时间复杂度:

    O(log(M + N))

  空间复杂度:

    O(log(M + N))

代码

 1 class Solution:
 2     def findMedianSortedArrays(self, nums1, nums2):
 3         """
 4         :type nums1: List[int]
 5         :type nums2: List[int]
 6         :rtype: float
 7         """
 8         m = len(nums1)
 9         n = len(nums2)
10         s = m + n + 1
11         return (self.findKth(nums1, nums2, s // 2) + self.findKth(nums1, nums2, s - s // 2)) / 2
12
13     def findKth(self, nums1, nums2, k):
14         m = len(nums1)
15         n = len(nums2)
16         if (m < n):
17             return self.findKth(nums2, nums1, k)
18         if (not n):
19             return nums1[k - 1]
20         if (k == 1):
21             return min(nums1[0], nums2[0])
22         mid1 = max(k * m // (m + n) - 1, 0)
23         mid2 = k - mid1 - 2
24         if (nums1[mid1] < nums2[mid2]):
25             return self.findKth(nums1[mid1 + 1 :], nums2[: mid2 + 1], k - mid1 - 1)
26         if (nums1[mid1] > nums2[mid2]):
27             return self.findKth(nums1[: mid1 + 1], nums2[mid2 + 1 :], k - mid2 - 1)
28         return nums1[mid1]

原文地址:https://www.cnblogs.com/Efve/p/9062352.html

时间: 2024-08-30 05:06:13

两个排序数组的中位数的相关文章

LeetCode-4. 两个排序数组的中位数(详解)

链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] nums2 = [2] 中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位数是 (2 + 3)/2 = 2.

Leetcode(4)-两个排序数组的中位数

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] nums2 = [2] 中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位数是 (2 + 3)/2 = 2.5 自己的思路:既然两个数组都是有序的,我可以将两个数组合并成一个数组,依然有序,然后根据奇偶来判断出中位数. double findMedianSo

LeetCode刷题-004两个排序数组的中位数

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1:nums1 = [1, 3]nums2 = [2]中位数是 2.0 示例 2:nums1 = [1, 2]nums2 = [3, 4]中位数是 (2 + 3)/2 = 2.5 1 class Solution { 2 public: 3 double findMedianSortedArrays(vector<int>&

[LeetCode] 4. 两个排序数组的中位数

该题的难度分级是Hard,那么难在哪里呢?我们先来看题目. 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] nums2 = [2] 中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位数是 (2 + 3)/2 = 2.5 当你看到要求的时间复杂度为O(log (m+n)),你想到了什么?没错,二分法. 从示

leetcode链表--13、median-of-two-sorted-arrays(两个排序数组的中位数,时间复杂度)

题目描述 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 题目解析:本题关键之处在于时间复杂度要求为O(log(m+n)),本题如果采用合并并排序两数组的方式,时间复杂度为O((m+n)*log(n+m))但是在牛客网上

两个排序数组求中位数

题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 思路 丢掉一个最小的,丢掉一个最大的 如果剩下小于等于两个数字,可以从中得到中位数 标准写法不是这样的,是通过寻找第K小的数来实现的 该方法的核心是将原问题转变成一个

[LintCode] 两个排序数组的中位数

1 class Solution { 2 public: 3 /** 4 * @param A: An integer array. 5 * @param B: An integer array. 6 * @return: a double whose format is *.5 or *.0 7 */ 8 double findMedianSortedArrays(vector<int> A, vector<int> B) { 9 // write your code here

[LeetCode]4. Median of Two Sorted Arrays两个排序数组合并后的中位数

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解法一:不考虑时间复杂度限制在O(log(m+n)),则将两个数组遍历一遍即可以组合成一个排好序的数组,然后取数组的中位数即可,时间复杂度O(m+n): c

[LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 求两个有序数组的中位数,并限制了时间复杂度O(log (m+n)),看到这个时间复杂度,自然想到用二分搜索Binary Search. 对于一个长度为n的已