给定两个有序数组arr1和arr2,在给定一个整数k,返回两个数组的所有数中第K小的数。
例如:
arr1 = {1,2,3,4,5};
arr2 = {3,4,5};
K = 1;
因为1为所有数中最小的,所以返回1;

arr1 = {1,2,3};
arr2 = {3,4,5,6};
K = 4;
因为3为所有数中第4小的数,所以返回3;

要求:如果arr1的长度为N,arr2的长度为M,时间复杂度请达到O(log(min{M,N}))。

这题目的难度在于时间复杂度请达到O(log(min{M,N})),参考http://www.cnblogs.com/diegodu/p/3790860.html可以达到O(log(M+N))

借助http://www.cnblogs.com/diegodu/p/4589847.html 求等长数组求中位数的方法能够实现。

举例,

arr1  1 ~ 10,

arr2  1‘ ~ 20’.

当kth = 7时,kth < min(s1,s2),

  求两个数组的前K个元素的中位数即可,因为,第K个必然在两个数组的前K个元素中。

  getUpMedian(arr1, 0, kth - 1, arr2, 0, kth - 1);

当kth = 26时,kth>max(s1,s2)

  arr1 的1到5 是不可能的,就算5>20,也不过第25而已,不会是26.

   arr2的 1’到15‘ 是不可能的,就算15’>10,也不过第25而已,不会是26.

  所以排除了5+15个元素,arr1剩下5个,arr2剩下5个。

  如果求剩下10个元素的上中位数,我们能得到第25大的,不是第26大,

所以,我们可以单独检验一下arr1 的6 和arr2的16‘是不是答案,

若是直接返回,不是的话又排除了两个,排除了22个,剩余8个,求上中位数第四个,加一起正好是第26个。

当kth = 16时,min(s1, s2)<kth<max(s1,s2)

arr1 无法排除,都有可能。

arr2的 1’~5‘排除,就算5’大于arr1的10,也不过是15。

arr2的 17‘~20’排除,17‘本来就排17了,最小也就是拍17。

所以arr2还剩下 6’ ~ 16‘ 11个元素,而arr1 有10个元素,

为了使用上中位数的算法,可以单独检测一下6’,若是返回,若不是淘汰,剩余的使用上中位数的算法。

http://www.nowcoder.com/profile/864393/test/231563/24588

class Solution {
public:
int getUpMedian(vector<int> arr1, vector<int> arr2) { if(arr1.size() != arr2.size())
return -;
if(arr1.size() == )
return -; return getUpMedian(arr1, , arr1.size() -,
arr2, , arr1.size() - );
} int getUpMedian(const vector<int> & arr1, int start1, int end1,
const vector<int> & arr2, int start2, int end2)
{
//cout << "start1\t" << start1 << endl;
//cout << "end1\t" << end1 << endl;
//cout << "start2\t" << start2 << endl;
//cout << "end2\t" << end2 << endl; if(start1 == end1)
{
return min(arr1[start1], arr2[start2]);
} int size = end1 - start1 + ;
int halfSize;
if(size & 0x1 == 0x1)
{
halfSize = (size + )/;
}
else
{
halfSize = size/;
} if(arr1[start1 + halfSize - ] == arr2[start2 + halfSize - ])
return arr1[start1 + halfSize - ];
else if(arr1[start1 + halfSize - ] > arr2[start2 + halfSize - ])
return getUpMedian(arr1, start1, start1 + halfSize - ,
arr2, end2-(halfSize-), end2);
else //if(arr1[start1 + halfSize - 1] > arr2[start2 + halfSize - 1])
return getUpMedian(arr1, end1-(halfSize-) , end1,
arr2, start2, start2 + halfSize -);
} int findKthNum(vector<int> arr1, vector<int> arr2, int kth)
{
int size1 = arr1.size();
int size2 = arr2.size(); if(size1 > size2)
return findKthNum(arr2, arr1, kth);//ensure size1 < size2 if(kth <= size1)
return getUpMedian(arr1, , kth - , arr2, , kth - );
else if (kth <= size2 && kth > size1)
//i.e.s1 = 10, s2 = 20, kth = 14;
// for arr2, kth + 1 ~ size2(15 ~ 20) is impossibly answer
// for arr2, 1 ~ kth - size1 -1 (1 ~ 3) is impossibly answer
// so arr2 has 20 - 6 - 3 = 11 numbers, but arr1 has 10 numbers
// so jundge one element of arr2 firstly, the call getUpMedian
{
if(arr2[kth - size1 - ] >= arr1[size1 - ] )
return arr2[kth - size1 - ];
return getUpMedian(arr1, , size1-, arr2, kth-size1, kth-);
} else //if (kth > size2)
//i.e.s1 = 10, s2 = 20, kth = 25;
// first 4(kth - s2 - 1) of arr1 is impossibly answer. so just remove them
// first 14(kth - s1 - 1) of arr2 is impossibly answer.so just remvoe them
// arr1 still has s1 - (kth - s2 - 1) = s1 + s2 - kth + 1 numbers (6)
// arr2 still has s2 - (kth - s1 - 1) = s1 + s2 - kth + 1 numbers (6)
// we removes 2 * kth - s1 - s2 -2 numbers, (18)
// now we junge if arr1[kth-s2-1] and arr2[kth-s1-1] is the answer,if not, we remother them
// then we removes 2 * kth - s1 - s2 numbers (20)
// and arr1 and arr2 both have s1 + s2 - kth numbers(5)
// we just calc upMedia of them and got the answer, 20 + 5 = 25
{
if(arr2[kth-size1-] >= arr1[size1-])
return arr2[kth-size1-];
if(arr1[kth-size2-] >= arr2[size2-])
return arr1[kth-size2-];
return getUpMedian(arr1, kth-size2, size1-, arr2, kth-size1, size2-);
} }
};

[nowCoder] 两个不等长数组求第K大数的更多相关文章

  1. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  2. 无序数组求第K大的数

    问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...

  3. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  4. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. HDU 5249 离线树状数组求第k大+离散化

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 寻找数组中第K大数

    1.寻找数组中的第二大数 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...

  7. 求第K大数(分治)

    题意:已知N个数,求第K大数. 分析: 1.复杂度O(n). 2.利用快排中划分的原理,每次划分以序列中第一个数x为标准,将序列划分为{比x大的数}x{比x小的数}. 3.若集合{比x大的数}中元素为 ...

  8. poj3261 后缀数组求重复k次可重叠的子串的最长长度

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13669   Accepted: 6041 Ca ...

  9. 计算两个有序数组的第K大数(转)

    传统解法,最直观的解法是O(m+n).直接merge两个数组,然后求第K大的数字. 如果想要时间复杂度将为O(log(m+n)).我们可以考虑从K入手.如果我们每次能够删除一个一定在第K个元素之前的元 ...

随机推荐

  1. RSA 加解密

    #region RSA public static byte[] GetBytes(String num) { BigInteger n = ); String s = n.ToString(); & ...

  2. AMQ学习笔记 - 17. 事务的测试

    概述 对事务机制进行测试. 测试实例 测试实例 结果预测 发送正常 3条消息入队 发送异常 0条消息入队 接收正常 3条消息出队 接收异常 0条消息出队 demo设计 设计图 测试分工 测试类 测试方 ...

  3. 利用CSS3打造一组质感细腻丝滑的按钮

    CSS3引入了众多供功能强大的新特性,让设计和开发人员能够轻松的创作出各种精美的界面效果.下面这些发出闪亮光泽的按钮,漂亮吧?把鼠标悬停在按钮上,还有动感的光泽移动效果. 温馨提示:为保证最佳的效果, ...

  4. Ajax乱码问题

    Ajax中文乱码问题   1.js代码 var userName=$("#userName").val(); var url = "user/login.action?u ...

  5. SilverLight命名空间详解-新手入门

    1.核心命名空间 1.xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"是silverlight的核 ...

  6. GridView、DataGrid、DataList、Repeater、ListView、DetailsView、FormView

    列表类   GridView 控件   GridView 控件以表的形式显示数据,并提供对列进行排序.分页.翻阅数据以及编辑或删除单个记录的功能.   特征:一行一条记录,就像新闻列表一样:带分页功能 ...

  7. 探索VS中C++多态实现原理

    引言 最近把<深度探索c++对象模型>读了几遍,收获甚大.明白了很多以前知其然却不知其所以然的姿势.比如构造函数与拷贝构造函数什么时候被编译器合成,虚函数.实例函数.类函数的区别等等.在此 ...

  8. c++11:copy_n

    copy_n: Copies exactly count values from the range beginning at first to the range beginning at resu ...

  9. 每日一“酷”之heapq

    作用:heapq模块实现一个适用于Python列表的最小堆排序算法 堆(heap)是一个属性数据结构,其中子节点与父节点是一种有序关系.二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全 ...

  10. 内部技术分享的 PPT

    本文的基础是搞了一次内部的技术分享,在此也分享一下本次的PPT的一些内容.先列一下大概内容吧. EF-Code First API(WCF.WebAPI) Xaml MVVM AOP Xamarin. ...