这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低。。。

题目:

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)).

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5

思路是:

对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1] ,
若n为偶数,则中位数(a[n / 2] + a[n / 2 + 1]) / 2
如果我们可以在两个数列中求出第K小的元素,便可以解决该问题
不妨设数列A元素个数为n,数列B元素个数为m,各自升序排序,求第k小元素
取A[k / 2] B[k / 2] 比较,
如果 A[k / 2] > B[k / 2] 那么,所求的元素必然不在B的前k / 2个元素中(证明反证法)
反之,必然不在A的前k / 2个元素中,于是我们可以将A或B数列的前k / 2元素删去,求剩下两个数列的
k - k / 2小元素,于是得到了数据规模变小的同类问题,递归解决
如果 k / 2 大于某数列个数,所求元素必然不在另一数列的前k / 2个元素中,同上操作。

时间复杂度为log(m + n)的答案:

class Solution {
public:
int getkth(int s[], int m, int l[], int n, int k){
// let m <= n
if (m > n)
return getkth(l, n, s, m, k);
if (m == 0)
return l[k - 1];
if (k == 1)
return min(s[0], l[0]); int i = min(m, k / 2), j = min(n, k / 2);
if (s[i - 1] > l[j - 1])
return getkth(s, m, l + j, n - j, k - j);
else
return getkth(s + i, m - i, l, n, k - i);
return 0;
} double findMedianSortedArrays(int A[], int m, int B[], int n) {
int l = (m + n + 1) >> 1;
int r = (m + n + 2) >> 1;
return (getkth(A, m ,B, n, l) + getkth(A, m, B, n, r)) / 2.0;
}
};

  

算法题之Median of Two Sorted Arrays的更多相关文章

  1. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

  2. 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 ...

  3. 刷题4. Median of Two Sorted Arrays

    一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...

  4. 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively.  Find the median of the tw ...

  5. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  6. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  7. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  8. LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

随机推荐

  1. jzoj3865[JSOI2014]士兵部署

    ‘ 数据范围:n,m<=10^5,传送门:https://jzoj.net/senior/#main/show/3865 感觉jzoj好高明啊,就是访问不太稳定. 首先题意中被n个点控制的区域相 ...

  2. 【bzoj4550】小奇的博弈 博弈论+dp

    题目描述 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色.最左边是白色棋子,最右边 是黑色棋子,相邻的棋子颜色不同.   小奇可以移动白色棋子,提比可以移动黑色的棋子, ...

  3. linux文件服务器:samba服务器

    windows上,需要和linux虚拟机进行方便的文件交互,总结一下遇到的问题. 1.samba简介 windows和windows之间共享文件可以用“网上邻居”,linux和linux间共享文件用 ...

  4. [洛谷P4900]食堂

    题目大意:$n(n\leqslant10^6)$组询问,每组询问给出$l,r(l,r\leqslant10^6)$,求($\{\dfrac ij\}$表示$\dfrac ij$的小数部分): $$\s ...

  5. ZOJ1994 & POJ2396:Budget——题解

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1994 http://poj.org/problem?id=2396 题目大 ...

  6. php 在线预览word

    一般类似oa或者crm等管理系统可能都会遇到需要再线查看word文档的功能,类似百度文库. 记得去年小组中的一个成员负责的项目就需要这个的功能,后面说是实现比较困难,就将就着用chm格式替代了.今天看 ...

  7. PowerDesigner 快捷键

    一般快捷键 快捷键 说明 F4 打开检查模型窗口,检查模型 F5 如果图窗口内的图改变过大小,恢复为原有大小即正常大小 F6 放大图窗口内的图 F7 缩小图窗口内的图 F8 在图窗口内中查看全部图内容 ...

  8. 关于android中PendingIntent.getBroadcase的注册广播

    使用语句 PendingIntent intent= PendingIntent.getBroadcast(Context context, int requestCode, Intent inten ...

  9. 【Android】完善Android学习(一:API 2.3.3)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  10. CSS hack浏览器兼容一览表

    CSS hack是指我们为了兼容各浏览器,而使用的特别的css定义技巧.这是国外摘来的一张CSS hack列表,显示了各浏览器对css hack的支持程度,对我们制作兼容网页非常有帮助.