题目: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. Python Unicode 转换 字符串

    estimate_price = "\u00a340\u00a0\u00a0-\u00a060" sold_price = "Sold for \u00a345" ...

  2. NTP Reply Flood Attack (NTP反射型DDos攻击)

    简介 NTP Reply Flood Attack (NTP射型Ddos攻击)以下简称NTP_Flood是一种利用网络中NTP服务器的脆弱性(无认证,不等价数据交换,UDP协议),来进行DDos行为的 ...

  3. 0x01 译文:Windows桌面应用Win32开发简介

    本节课将简单介绍下使用C++开发Windows桌面应用的一些基础知识 目录: 准备你的开发环境 Windows 代码规范 操作字符串 什么是一个Window? WinMain:程序的入口点 1. 准备 ...

  4. Flume 中文入门手冊

    原文:https://cwiki.apache.org/confluence/display/FLUME/Getting+Started 什么是 Flume NG? Flume NG 旨在比起 Flu ...

  5. redmine 的安装

    https://bitnami.com/stack/redmine/installer#官方地址 安装很简单,给权限 chmod +x bitnami-redmine-3.3.0-1-linux-x6 ...

  6. 0071 CentOS_Tomcat访问文件名包含中文的文件出现404错误

    访问CentOS+Tomcat下的,文件名包含中文的文件出现404错误 修改:apache-tomcat-7.0.78/conf/server.xml <Connector port=" ...

  7. You-Get 视频下载工具 Python命令行下载工具

    You-Get 是一个命令行工具, 用来下载各大视频网站的视频, 是我目前知道的命令行下载工具中最好的一个, 之前使用过 youtube-dl, 但是 youtube-dl 吧, 下载好的视频是分段的 ...

  8. spark读取gz文件

    spark 1.5.1是支持直接读取gz格式的压缩包的,和普通文件没有什么区别: 使用spark-shell进入spark shell 交互界面: 输入命令: sc.textFile("\h ...

  9. oracle初始操作

    oracle登录 sqlplus  sys/oracle as sysdba 这个登录之后呢 会出现这个: Connected to an idle instance. 这一步是连接上 [oracle ...

  10. PHP——注册页面,审核页面,登录页面:加Session和Cookie

    实现效果: 用户注册信息,管理员核对信息审核通过后,可实现注册的用户名和密码的成功登陆,利用session和cookie获取用户信息并且不能跳过登录页面直接进入主页面 1.Session存储在服务器可 ...