题目来源:

https://leetcode.com/problems/median-of-two-sorted-arrays/


题意分析:

这道题目是输入两个已经排好序的数组(长度为m,n),将这两个数组整合成一个数组,输出新数组的中位数。要求时间复杂度是(log(m + n)。比如如果输入[1,2,3],[3,4,5]。那么得到的新数组为[1,2,3,3,4,5]得到的中位数就是3.


题目思路:

由于题目要求的时间复杂度是(log(m+n)),如果我们直接把两个数组整合一起,那么时间复杂度肯定超过(log(m+n))。所以整理肯定是不行的。那么还有什么方法吗?答案是肯定的。

首先我们要先了解中位数的概念,中位数就是有序数组的中间那个数。那么如果我们将比中位数小的数和比中位数大的数去掉同样的个数,中位数的值也不会变化(数组的个数为偶数的时候另外讨论,因为那时候中位数是中间两个数的平均值,所以中位数旁边两个数不能去掉)

所以我们不妨试着将数组长度不断缩短。这里不妨提出一个引理。假设有两个有序数组am,bn,他们整合后的有序数组为cn+m。他们的中位数分别是am/2,bn/2,c(m+n)/2。如果am/2 < bn/2,则 a0…m/2 <= c(m+n)/2 <= bn/2…n 。

引理证明:

假设 am/2 > c(m+n)/2 ,那么 bn/2  > c(m+n)/2,所以在数组am里有大于m/2个数大于c(m+n)/2,在数组bn里也有n/2个数大于c(m+n)/2

也就是说在cn+m里有(m+n)/2个数大于c(m+n)/2,此时就c(m+n)/2不再是数组cn+m的中位数。

所以a0…m/2 <= c(m+n)/2

同理可得c(m+n)/2 <= bn/2…n 

根据上述引理,我们不妨设m>n,那么我们根据判断两个数组的中位数大小,每个数组每次减少n/2长度,直到n为1。如此,我们通过减少log(n)次可以得到答案。这种方法的时间复杂度是(log(min(m,n)))。


代码(python):

  1. class Solution(object):
  2. def getMedian(self,nums):
  3. size = len(nums)
  4. if size == 0:
  5. return [0,0]
  6. if size % 2 == 1:
  7. return [nums[size // 2],size // 2]
  8. return [(float(nums[size // 2 - 1] + nums[size // 2])) / 2,size // 2]
  9.  
  10. def findMedianSortedArrays(self, nums1, nums2):
  11. """
  12. :type nums1: List[int]
  13. :type nums2: List[int]
  14. :rtype: float
  15. """
  16. size1 = len(nums1)
  17. size2 = len(nums2)
  18. if size1 < size2:
  19. return self.findMedianSortedArrays(nums2,nums1)
  20. m1 = self.getMedian(nums1)
  21. m2 = self.getMedian(nums2)
  22. if size2 == 0:
  23. return m1[0]
  24. if size2 == 1:
  25. if size1 == 1:
  26. return (float(nums1[0] + nums2[0])) / 2
  27. if size1 % 2 == 0:
  28. if nums2[0] < nums1[size1 //2 - 1]:
  29. return nums1[size1 // 2 - 1]
  30. if nums2[0] > nums1[size1 // 2 ]:
  31. return nums1[size1 // 2]
  32. else:
  33. return nums2[0]
  34. else:
  35. if nums2[0] < nums1[size1 // 2 - 1]:
  36. return (float(nums1[size1 // 2 - 1] + nums1[size1 // 2])) / 2
  37. if nums2[0] > nums1[size1 // 2 + 1]:
  38. return (float(nums1[size1 // 2] + nums1[size1 // 2 + 1])) / 2
  39. else:
  40. return (float(nums2[0] + nums1[size1 // 2])) / 2
  41. if size2 % 2 == 0:
  42. if size1 % 2 == 0:
  43. if nums2[size2 // 2 - 1] < nums1[size1 //2 - 1] and nums2[size2 // 2] > nums1[size1 // 2]:
  44. return m1[0]
  45. if nums1[size1 // 2 - 1] < nums2[size2 // 2 - 1] and nums1[size1 // 2] > nums2[size2 // 2]:
  46. return m2[0]
  47. if m1[0] < m2[0]:
  48. return self.findMedianSortedArrays(nums1[m2[1]:],nums2[:size2 - m2[1]])
  49. if m1[0] > m2[0]:
  50. return self.findMedianSortedArrays(nums1[:size1 - m2[1]],nums2[m2[1]:])
  51. else:
  52. return m1[0]

PS:当两个数组长度都是偶数的时候,由于中位数和中间两个数相关,如果直接删减有可能把中位数的数值发生改变,比如:[1,6],[4,5],这种情况如果用上述算法,那么先得到[1],[4]最后得到2.5,然后最后答案应该是4.5,所以这种情况要另外讨论。

还有,用python3.0要注意语法的不同,因为判断系统是用2.7的算法。3.0的’/’默认是浮点数,而在‘2.7’如果原来是整型的时候是整除。


转载请注明出处:http://www.cnblogs.com/chruny

[LeetCode]题解(python):004-Median of Two Sorted Arrays的更多相关文章

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

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

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

  3. No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  4. LeetCode--No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

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

  6. 【LeetCode】004. Median of Two Sorted Arrays

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

  7. 【JAVA、C++】LeetCode 004 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 ...

  8. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  9. 004 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 ...

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

随机推荐

  1. 安装TDM-GCC

    TDM-GCC是一组免费的编译器套件,有32位和64位两种版本.其中64位版既可以编译生成64位的可执行文件,又可以编译生成32位的可执行文件.从TDM-GCC的官网可以下载到相应的安装包,安装完成后 ...

  2. mysql+php+pdo批量添加大数据

    1.使用insert into插入 ini_set('max_execution_time','0');//限制超时时间,因为第一种时间较长,索性设为0不限制 $pdo = new PDO(" ...

  3. Python 整理一

    ---恢复内容开始--- Python (pailen)最近学习这个语言,其实早在几年前学习perl的时候就知道有这个语言了,在讲perl的那本书后面就推荐学习python,并且还附加了二章的入门.当 ...

  4. Android的消息机制

    一.简介 ①.我们不能在子线程中去访问UI空控件,这是时候只能通过Handler将更新UI的操作放到主线程中去执行 ②.Handler的组成:messageQueue和Looper的支持 ③.Mess ...

  5. SquirrelMQ消息队列

    SquirrelMQ是一个快速的消息队列. SquirrelMQ特性: 1. SquirrelMQ使用Slab内存分配算法来降低内存碎片,使用epoll来解决高并发问题.效率比redis要高,使用简单 ...

  6. 必学100个常用linux命令大全

    1,echo “aa” > test.txt 和 echo “bb” >> test.txt //>将原文件清空,并且内容写入到文件中,>>将内容放到文件的尾部 2 ...

  7. 全选demo

    我们处理数据时,最好能够支持全选操作. 选中之后,进行删除,或其他处理. 我自己写了一个demo. 主要功能: 1.点击全部选中 2.点击全部取消 3.然后进行获取选中的id,进行处理 代码如下: & ...

  8. JavaEE Tutorials (25) - 使用Java EE拦截器

    25.1拦截器概述380 25.1.1拦截器类381 25.1.2拦截器生命周期381 25.1.3拦截器和CDI38125.2使用拦截器381 25.2.1拦截方法调用382 25.2.2拦截生命周 ...

  9. Easyui几种布局方式的使用

    1.通过标记创建layout.  记得添加"easyui-layout"样式给div标记. <div id="cc" class="easyui ...

  10. poj 2704 Pascal's Travels_记忆化搜索

    一道简单但是题意蛋疼的题目 题意:给你个n*n的图,开始在左上角,要求走到右下角有多种走法,图上的数表示走几步,只能向右或向下走. #include<iostream> #include& ...