有序数组求中位数问题

这篇博客探讨了如何在O(logn)的时间复杂度内解决已排好序的数组A和B合并后的中位数问题。通过分析算法导论的方法,博主解释了如何利用二分查找的思想,根据数组元素比较结果缩小搜索范围,实现递归或非递归的解决方案。文章还包含了针对数组长度不等时的处理方法,并提供了具体的代码实现。
摘要由CSDN通过智能技术生成
1、有两个已排好序的数组A和B,长度均为n,找出这两个数组合并后的中间元素,要求时间代价为O(logn)。
2、假设两个有序数组长度不等,同样的求出中位数。
一:解析: 这个题目看起来非常简单。第一题的话: 假设数组长度为n, 那么我就把数组1和数组2直接合并,然后再直接找到中间元素。对于这样的方案,第一题和第二题就没有什么区别了。这样的话时间复杂度就是O(n)。通常在这样的情况下,那些要求比较高的面试官就会循循善诱道:“你还有更好的办法吗?” 如果比线性更高效,直接能想到的就是对数了O(log(n)),这个时间复杂度在这里可能吗? 当然还是可能的。
算法导论上面的分析是这样的:
Say the two arrays are sorted and increasing, namely A and B.
It is easy to find the median of each array in O(1) time.
Assume the median of array A is m and the median of array B is n. Then,
1、If m==n,then clearly the median after merging is also m,the algorithm holds.
2、If m<=n,then reserve the half of sequence A in which all numbers are greater than m,also reserve the half of sequence B in which all numbers are smaller than n.
Run the algorithm on the two new arrays。
3、If m>n,then reserve the half of sequence A in which all numbers are smaller than m,also reserve the half of sequence B in which all numbers are larger than n.
Run the algorithm on the two new arrays。
Time complexity: O(logn)
假设有两个有序数组 nums1 和 nums2,长度分别为 m 和 n。要找到这两个数组中位数,时间复杂度要为 O(log(m+n))。 一种思路是利用归并排序的思想,将两个有序数组合并成一个有序数组,然后找到中位数。 具体步骤如下: 1. 初始化两个指针 i 和 j,分别指向 nums1 和 nums2 的起始位置。 2. 判断两个指针所指元素的大小关系,将较小的元素加入到一个新的数组,并将指针向后移动一位。 3. 重复步骤 2,直到任意一个指针越界。 4. 判断剩余数组的长度,将剩余的元素加入到新的数组。 5. 找到新数组中位数,如果新数组长度为偶数,则取间两个数的平均值,如果长度为奇数,则取间的数。 代码实现如下: ``` double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m = nums1.size(), n = nums2.size(); int total = m + n; vector<int> nums(total); int i = 0, j = 0, k = 0; while (i < m && j < n) { if (nums1[i] < nums2[j]) { nums[k++] = nums1[i++]; } else { nums[k++] = nums2[j++]; } } while (i < m) { nums[k++] = nums1[i++]; } while (j < n) { nums[k++] = nums2[j++]; } if (total % 2 == 0) { return (nums[total / 2 - 1] + nums[total / 2]) / 2.0; } else { return nums[total / 2]; } } ``` 时间复杂度为 O(m+n),不符合题目要。可以使用二分查找的方法将时间复杂度优化到 O(log(m+n))。 具体思路如下: 1. 假设两个有序数组的长度分别为 m 和 n,将 nums1 分为两部分,前一部分包含 i 个元素,后一部分包含 m-i 个元素;将 nums2 分为两部分,前一部分包含 j 个元素,后一部分包含 n-j 个元素。 2. 如果中位数在两个数组的左半部分,那么 i 和 j 都需要向右移动;如果在右半部分,i 和 j 都需要向左移动;如果 i 和 j 恰好满足条件,则找到了中位数。 3. 不断重复步骤 2,直到找到中位数。 代码实现如下: ``` double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m = nums1.size(), n = nums2.size(); if (m > n) { swap(nums1, nums2); swap(m, n); } int left = 0, right = m, halfLen = (m + n + 1) / 2; while (left <= right) { int i = (left + right) / 2; int j = halfLen - i; if (i < m && nums2[j-1] > nums1[i]) { left = i + 1; } else if (i > 0 && nums1[i-1] > nums2[j]) { right = i - 1; } else { int maxLeft = 0; if (i == 0) { maxLeft = nums2[j-1]; } else if (j == 0) { maxLeft = nums1[i-1]; } else { maxLeft = max(nums1[i-1], nums2[j-1]); } if ((m + n) % 2 == 1) { return maxLeft; } int minRight = 0; if (i == m) { minRight = nums2[j]; } else if (j == n) { minRight = nums1[i]; } else { minRight = min(nums1[i], nums2[j]); } return (maxLeft + minRight) / 2.0; } } return 0.0; } ``` 时间复杂度为 O(log(min(m,n)))。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值