题目: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)).

  刚开始的时候理解有误,以为是求中位数。其实不是。这里所谓的"median of the two sorted arrays"指的是:如果A.length + B.length 是奇数,返回merge后中间的那个数;如果是偶数,返回中间两个数的平均数。

例如:[1,2]和[3],返回2;而[1,2]和[3,4],返回2.5

  解法:不难有O(m+n)的做法,即merge A 和 B,得到一个长的sorted数组,返回中间一个或中间两个的平均数。但是不符合题目要求。

  如何做到O(log(m+n))呢?

   首先考虑如何找到在A和B数组merge的第k个元素。可以对k进行分治。假设A的长度为m,B的长度为n,m<n,A的offset为aoffset,B的offset为boffset,初始为0

  1. 如果m为0,返回B[k-1];
  2. 如果k是1,返回A[aoffset]和B[boffset]中比较小的那个;
  3. 我们从A中取前(相对于其offset的)Ka个(一般是k/2),从B中取前Kb个(Ka + Kb = k);
  4. 比较A[aoffset + Ka-1]和B[boffset + Kb-1]。如果A[aoffset + Ka-1] 大于B[boffset + Kb-1],说明要找的第k个元素肯定不在B中刚刚选取的Kb个元素中,否则的话,在选择到的总共K个数中,最大的数应该在B中(但是实际在A中);反之,第k个元素肯定也不会在A中选取的Ka个元素中。也可以这样想,通过比较A[aoffset + Ka-1]和B[boffset + Kb-1],我们知道在第3步中对K值的划分是否足够“偏向”目标元素,从而排除掉一部分元素;
  5. 如果A[aoffset + Ka-1] 大于B[boffset + Kb-1],移动boffset到boffset+kb,同时k减去Kb。回到1.(相当于继续在A和新的B中寻找第k-Kb个元素)
  6. 如果A[aoffset + Ka-1] 小于等于B[boffset + Kb-1],移动aoffset到aoffset+ka,同时k减去Ka。回到1.

  代码如下: 

 private static int findKth(int A[], int aoffset, int m, int B[],int boffset, int n,int k){
if(m > n) return findKth(B, boffset, n, A, aoffset, m, k);
if(m==0)return B[k-1];
if(k==1)return Math.min(A[aoffset], B[boffset]);
int pa = Math.min(k/2, m);
int pb = k - pa;
if(A[aoffset + pa - 1] >= B[boffset + pb - 1])
return findKth(A,aoffset, m, B, boffset + pb, n - pb, k-pb);
else return findKth(A, aoffset + pa, m - pa, B, boffset, n, k - pa);
}

findKth

注意在递归调用之前,始终保持m<n。

有了这个方法,我们可以用它来计算两个有序数组的第K个元素,当然也包括中间的一个(或者两个的平均值):

 public static double findMedianSortedArrays(int A[], int B[]) {
// Start typing your Java solution below
// DO NOT write main() function
//return findMiddleValue(A,B);//this is o(m+n)
int n = A.length + B.length;
if(n%2 == 0){
return (double)(findKth(A,0,A.length, B,0,B.length,n/2) +findKth(A,0,A.length, B,0,B.length,n/2 + 1))/(double)2;
}else {
return findKth(A,0,A.length, B,0,B.length,n/2 + 1);
}
}

findMedianSortedArrays

LeetCode 笔记系列一 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 二人 渣渣选手乱七八糟分析发现基本回到思路1

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

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

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

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

  5. LeetCode 笔记系列九 Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  6. leetcode 第4题 Median of Two Sorted Arrays

    class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int&g ...

  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. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

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

随机推荐

  1. unity, water cube

    <纪念碑谷>里有一关开始是一个宝箱展开后里面有一个water cube,其中还有小鱼在游.如下截图: 因为我们知道<纪念碑谷>是unity做的,而现在正开始学unity,所以也 ...

  2. CentOS 5.4 安装和卸载桌面

    显示系统已经安装的组件,和可以安装的组件:#yum grouplist 如果系统安装之初采用最小化安装,没有安装xwindow,那么先安装:#yum groupinstall "X Wind ...

  3. CYQ学习主要摘要

    这个个人搞的框架不知道到底怎么样,做个项目测试下看看,本来想用EF的,大多数人说性能低,所以先放弃,试试这个,一些主要地方我做个摘录 5:多表怎么搞定? A:视图方式,将多表查询放到数据库里成视图,情 ...

  4. Visual Studio:error MSB8020

    状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...

  5. PE框架学习

    PE开发基础: 开发平台PowerEngine: 开发新功能: 业务逻辑处理: 1.Transaction:交易 2.Chain:链.责任链 3.Command:命令 4.Template:模板 5. ...

  6. ashx上传姿势

    很多情况下网上流传的cer,asa,cdx……等等来上传,其实很多时候是不行的.但是ashx会好一些. PS:其实这个姿势也蛮老了,但是还是相当一大部分站点存在.也是一个姿势所以写博文记住它.免得忘了 ...

  7. cocos2dx中的一些坑

    1.CCTableView中的lua绑定LUA_TableViewDataSource 在TestLua里有例子,有个TableView的例子function TableViewTestLayer.c ...

  8. centos7系统根目录扩容

    比如 点击了后 点击创建虚拟磁盘 选择一个 20G  然后启动虚拟机使用fdisk查看所有的磁盘 看是否新增了一个20G的硬盘 [root@localhost ~]# fdisk -l 磁盘 /dev ...

  9. android system.img 解压和打包

    system.img重新编译的时间太长,添加和更改的文件系统内容,往往通过对system.img加压再打包的方式. 参考链接 http://blog.csdn.net/whu_zhangmin/art ...

  10. 第二百七十九节,MySQL数据库-pymysql模块操作数据库

    MySQL数据库-pymysql模块操作数据库 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connec ...