【leetcode】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)),就很有难度了。不过首先我们还是可以明确我们要用分治法。关键是怎么分治呢?我们有如下的思路(核心思想是求第k小):
class Solution:
def findk(self, A, m, B, n, k):
if m > n:
return self.findk(B, n, A, m, k)
if m == 0:
return B[k-1]
if k == 1:
return min(A[0],B[0])
pa = min(k / 2, m)
pb = k - pa
if A[pa - 1] < B[pb - 1]:
return self.findk(A[pa:], m - pa, B, n, k - pa)
elif A[pa - 1] > B[pb - 1]:
return self.findk(A, m, B[pb:], n - pb, k - pb)
else:
return A[pa - 1]
# @return a float
def findMedianSortedArrays(self, A, B):
la = len(A)
lb = len(B)
l = la + lb
if l % 2 == 0:
return (self.findk(A, la, B, lb, l/2+1) + self.findk(A, la, B, lb, l/2))/2.0
else:
return self.findk(A, la, B, lb, l/2+1)
很容易证明这个想法的复杂度正是O(log(m+n))
【leetcode】Median of Two Sorted Arrays的更多相关文章
- 【leetcode】Median of Two Sorted Arrays(hard)★!!
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 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 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- leetCode之Median of Two Sorted Arrays
[题目描述] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...
- 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 (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
随机推荐
- php程序 注册机制
密码加密方式 1.直接md5加密 2.md5和随机数(也可以是固定参数,如(Rfd4WE784)) 3.md5(md5) 4. 参考 function sp_password($pw,$authcod ...
- Excel,2010,可以独立打开窗口
HKEY_CLASSES_ROOT \ Excel.Sheet.12和HKEY_CLASSES_ROOT\Excel.Sheet.8 首先更改HKEY_CLASSES_ROOT \ Excel.She ...
- 解决自定义leftBarButtonItem返回手势失效的方法
考虑到interactivePopGestureRecognizer也有delegate属性,替换默认的self.navigationController.interactivePopGestureR ...
- 室内定位系列(三)——位置指纹法的实现(KNN)
位置指纹法中最常用的算法是k最近邻(kNN):选取与当前RSS最邻近的k个指纹的位置估计当前位置,简单直观有效.本文介绍kNN用于定位的基本原理与具体实现(matlab.python). 基本原理 位 ...
- js中获取窗口高度的方法
取窗口滚动条滚动高度 function getScrollTop() { var scrollTop=0; if(document.documentElement&&document. ...
- CH模拟赛 皇后游戏
/* 做的时候手推了一下n=2的四种情况,再排一下序就可以了,证明不是很严谨,但我想这就行了,毕竟全是套路 */ #include<iostream> #include<cstdio ...
- JavaMail: SSL vs TLS vs STARTTLS
SSL vs TLS vs STARTTLS There's often quite a bit of confusion around the different terms SSL, TLS an ...
- Mysql字符转义
在字符串中,某些序列具有特殊含义.这些序列均用反斜线('\')开始,即所谓的转义字符.MySQL识别下面的转义序列: \0 ASCII 0(NUL)字符. \' 单引号('''). \" 双 ...
- jquery 中jsonp的实现原理
在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,即一般的 ajax是不能进行跨域请求的.但 img.iframe .script等标签是个例外,这些标签可以通过 src属性请求到其 ...
- Windows 10(64位)配置Caffe运行环境的基本流程
最新博客地址已搬家到: http://blog.csdn.net/zzlyw/article/details/53215148