第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays
给定两个有序的整数序列。求中位数,要求复杂度为对数级别。
通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二分算法求得两个序列中比它小的数,整体复杂度也是对数级别。但是代码实现较为困难。
换一个思路,我们把中位数不要当作一个数,而当作一个序列的划分。划分后,序列的左半部设为L,右半部设为R 满足max(L)<=min(R)且满足len(L)==len(R)
二分搜索这个划分即可。对于A+B的长度为奇数的情况,我们进行特殊处理,在划分时允许“借一位”。
其中一个序列为空则直接输出答案。
补充一个算法,对于两个无序的数列求中位数,《算法概论》中给出了线性的解法。通过类似快速排序的划分方法对数列进行划分,预测中位数可能存在的部分。
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums2.size()==0)
{
if(nums1.size()%2==0)return (double)(nums1[nums1.size()/2]+nums1[nums1.size()/2-1])/2.0;
else return (double)nums1[nums1.size()/2];
}
if(nums1.size()==0)
{
if(nums2.size()%2==0)return (double)(nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0;
else return (double)nums2[nums2.size()/2];
}
int len=(nums1.size()+nums2.size())/2;
bool flag=(nums1.size()+nums2.size())%2==1;
if(flag)len++;
int l=-1,r=min((int)nums1.size()-1,len-1);
while(true)
{
int i=(l+r)/2,ii;
ii=i+1;
int j=len-(i+1)-1,jj;
jj=j+1;
if(j>=(int)nums2.size()){l=i+1;continue;}
int l1=-2147483647,l2=-2147483647,r1=2147483647,r2=2147483647;
if(i>=0)l1=nums1[i];
if(j>=0)l2=nums2[j];
if(ii<nums1.size())r1=nums1[ii];
if(jj<nums2.size()) r2=nums2[jj];
if(flag&&l1>=l2)r1=min(l1,r1);
if(flag&&l2>l1)r2=min(r2,l2);
int maxa=max(l1,l2);int minb=min(r1,r2);
if(maxa<=minb){return (double)(maxa+minb)/2.0;}
if(l1>r2){r=i-1;continue;}
else{l=i+1;continue;}
}
}
};
第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)的更多相关文章
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- [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 ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
随机推荐
- 关于MD5解密网站。www.cmd5.com
第一次听说这个网站,本人的名字居然也能够被解密,而且还是需要付费取得明文! 大家知道,md5加密是我们常用的加密方式,这个加密方式的好处在于不可逆.而且任何环境下算出的密文应该都是相同的,所以在大家登 ...
- tarjan求强连通分量模板
什么是强连通分量? 百度百科 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(stro ...
- idea必选配置
参考: IDEA配置
- linux环境下时间的查看和修改
查看日期和时间date 查看时区date -R 查看UTC时间date -u 修改日期[root@centos ~]# date -s 20181230Sun Dec 30 00:00:00 EST ...
- Python爬虫入门教程: 半次元COS图爬取
半次元COS图爬取-写在前面 今天在浏览网站的时候,忽然一个莫名的链接指引着我跳转到了半次元网站 https://bcy.net/ 打开之后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 c ...
- 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb
[题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...
- Spring MVC学习总结(13)——Spring MVC集成Swagger时文档无法排序问题
添加排序属性: window.swaggerUi = new SwaggerUi({ ... apisSorter: "alpha", // can also ...
- java之比较两个日期大小----https://blog.csdn.net/dongfangbaiyun/article/details/51225469
https://blog.csdn.net/dongfangbaiyun/article/details/51225469 java之比较两个日期大小 最近又用到两个日期大小的比较,因此记录在此,方便 ...
- 《Noisy Activation Function》噪声激活函数(一)
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51736830 Noisy Activa ...
- Codeforces Round #232 (Div. 2) On Sum of Fractions
Let's assume that v(n) is the largest prime number, that does not exceed n; u(n) is the smallest pri ...