题目简述:

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的更多相关文章

  1. 【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 ...

  2. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

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

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

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

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

  5. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  6. leetCode之Median of Two Sorted Arrays

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

  7. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  8. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  9. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

随机推荐

  1. MySQL的if,case语句使用总结

    原文地址: http://outofmemory.cn/code-snippet/1149/MySQL-if-case-statement-usage-summary

  2. C#:泛型(Generic)

    前言:  此系列都为个人对C#的回顾,属于个人理解,新司机可参考.求老司机指点.如果有什么问题或不同见解,欢迎大家与我沟通! 目录:  泛型是什么 泛型的好处及用途 如何声明使用泛型 泛型类 泛型方法 ...

  3. Parse Fatal Error at line 4 column 43: 已经为元素 "web-app" 指定属性 "xmlns"。

    打开web.xml;最上面的web-app里面,看有没有重复的,重点关注xmlns="http://java.sun.com/xml/ns/javaee" ,如果重复,删去就好~~ ...

  4. PHP陷阱,一些注意事项

    判断的一些注意事项 count(false) > 0 // true count(0) > 0 // true "随便一个字符串" == 0 // true " ...

  5. 2016百度之星 资格赛ABCDE

    看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...

  6. centos7 cannot find a valid baseurl for repo base (转载)

    centos7 cannot find a valid baseurl for repo base     今天在虚拟机下安装centosmini版本,安装后第一件事就是yum update 但是有错 ...

  7. [Algorithm & NLP] 文本深度表示模型——word2vec&doc2vec词向量模型

    深度学习掀开了机器学习的新篇章,目前深度学习应用于图像和语音已经产生了突破性的研究进展.深度学习一直被人们推崇为一种类似于人脑结构的人工智能算法,那为什么深度学习在语义分析领域仍然没有实质性的进展呢? ...

  8. SpingMvc中的异常处理

    一.处理异常的方式      Spring3.0中对异常的处理方法一共提供了两种: 第一种是使用HandlerExceptionResolver接口. 第二种是在Controller类内部使用@Exc ...

  9. Java性能调优之:idea变慢解决

    今天搬砖的时候遇到一个问题,idea总是卡死,完全无法愉快的玩耍.幸好机器是Linux系统的.于是通过以下方式解决了问题: 通过top命令,查看系统运行状态发现4个CPU中有1个CPU用户占用率为10 ...

  10. UITableViewCell 的附件类型 accessoryType 选中、详情、箭头

    UITableViewCell * cell = [[UITableViewCell alloc] init]; 设置cell的附件类型: // >1 打钩 选中//    cell.acces ...