3.Median of Two Sorted Arrays Leetcode
难度系数:5星
/***************************************************************************
题目:有两个排好序的数组,要求其中位数,时间复杂度控制在O(log(m+n))
一般化:求两个排序数组的第K大的元素
直观思路1:PA和PB首先指向这两个子数组,对比比较,A小则PA++,m++,B小则
PB++,m++,直至m==k ======>> 时间复杂度O(m+n)
正解思路:
由于数组是有序的,又是有序的,能否通过二分等思路来可能筛掉一些元素
关键是设计一种方案让其收敛到最终位置
尝试二分: 若Amid<Bmid,cutA前一半,B后一半,
能确定的是A前一半小于A后一半以及B后一半。
A------------cut---------------
B------cut--------
那么A的前一半不可能是中位数,筛掉,要是Bmid小呢,只能筛掉B的前一半
这样不稳定
如果以K的一半为标准呢:
A[k/2-1]==B[k/2-1],说明AB的前k/2-1+k/2-1=k-2个元素在topK内
那么 A[k/2-1]便是第K-1和第K大的数(K为偶数)
A[K/2-1]>B[k/2-1] 去除掉A数组前K/2元素(实际最大能去除整个数组元素)
A[K/2-1]<B[k/2-1]
step1:当A或B是空时,直接返回A[K-1]或者B[k-1]
step2:当K=1,返回min[A[0],B[0]]
step3:当A[K/2-1]==B[k/2-1] 返回A[K/2-1]
************************************************************/
class Solution{
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){
int m = nums1.size(),n = nums2.size();
int total = m+n;
cout<<total<<endl;
if(total&0x1)
return find_k(&nums1[0],m,&nums2[0],n,total/2+1);
else
{
return (find_k(&nums1[0],m,&nums2[0],n,total/2)
+find_k(&nums1[0],m,&nums2[0],n,total/2+1))/2.0;
}
}
private:
static int find_k(int A[],int m,int B[],int n,int k)
{
if(m>n)return find_k(B,n,A,m,k);
if(m==0) return B[k-1];
if(k==1)return min(A[0],B[0]);
int ia = min(m,k/2),ib = k-ia;
if(A[ia-1]<B[ib-1]) return find_k(A+ia,m-ia,B,n,ib);
else if(A[ia-1]>B[ib-1])
return find_k(A,m,B+ib,n-ib,k-ib);
else return A[ia-1];
}
};
int main()
{
int a[]={1,3,5,7,9};
int b[]={2,4,6};
Solution S;
vector<int> B(b,b+3),A(a,a+5);
cout<<S.findMedianSortedArrays(A,B)<<endl;
return 0;
}
3.Median of Two Sorted Arrays Leetcode的更多相关文章
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 【算法之美】求解两个有序数组的中位数 — 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(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- mipi csi接口,1条lane支持多少像素,200w像素需要几条lane,为什么,怎么计算出来的?谢谢!
按帧频FRAME=60HZ, 分辨率480*800来计算;以WVGA 显示分辨率,24BIT图片,60幁为例,在理想状态下(未包含RGB信号前后肩宽度),总传输速率最小为:480*800*8BIT*3 ...
- C++链表常见面试考点
链表常见问题: 单链表找到倒数第n个节点 用两个指针指向链表头,第一个指针先向前走n步,然后两个指针同步往前走,当第一个指针指向最后一个节点时,第二个指针就指向了倒数第n个节点. 判断链表有没有环 快 ...
- c#复制数组的多种方法
方法一:使用for循环 int []pins = {9,3,7,2} int []copy = new int[pins.length]; for(int i =0;i!=copy.length;i+ ...
- 就因为把int改成Integer,第2天被辞了
本文节选自<设计模式就该这样学>之享元模式(Flyweight Pattern) 1 故事背景 一个程序员就因为改了生产环境上的一个方法参数,把int型改成了Integer类型,因为涉及到 ...
- SQL注入之猫舍
第一步:先查看是否存在注入点:构造?id=1 and 1=1 回车后发现页面正常 构造?id=1 and 1=2 发现页面异常,得出结论:存在注入点 第二步:判断字段数 当输入order by 1和o ...
- Mysql - 如何决定用 datetime、timestamp、int 哪种类型存储时间戳?
背景 数据表都很可能会有一两个字段需要保存日期时间数据,那应该用什么 Mysql 类型来保存呢? 前面讲过 datetime.timestamp.int 的方式来保存日期时间 如何存储 10位.13位 ...
- 问题 A: 喷水装置(一)
题目描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置, 每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i ...
- 到底谁才需要Service Mesh?
本文是Service Mesh系列第1篇 随着云原生时代的来临,使用微服务架构的朋友们开始听到一个新的技术名词--Service Mesh(现在来说已经不算新了). 对于一项新技术的学习,总归绕不过两 ...
- GO的安装以及GoLand破解
GO的安装以及GoLand破解 GO的安装 GO语言中文网:GO语言中文网 go,GoLand,破解文件:JetBrains GoLand 2019.2.3 x64 提取码:ABCD(汉化文件也在其中 ...
- 力扣 - 剑指 Offer 12. 矩阵中的路径
题目 剑指 Offer 12. 矩阵中的路径 思路1(回溯.DFS) 这题可以使用回溯+递归来解决,思路如下: 将二维数组的每一个元素都作为起点进行回溯查找 每次查找的时候,都有四个方向,但是上一个方 ...