[nowCoder] 两个不等长数组求第K大数
给定两个有序数组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大数的更多相关文章
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- 无序数组求第K大的数
问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArra ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 寻找数组中第K大数
1.寻找数组中的第二大数 using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- 求第K大数(分治)
题意:已知N个数,求第K大数. 分析: 1.复杂度O(n). 2.利用快排中划分的原理,每次划分以序列中第一个数x为标准,将序列划分为{比x大的数}x{比x小的数}. 3.若集合{比x大的数}中元素为 ...
- poj3261 后缀数组求重复k次可重叠的子串的最长长度
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13669 Accepted: 6041 Ca ...
- 计算两个有序数组的第K大数(转)
传统解法,最直观的解法是O(m+n).直接merge两个数组,然后求第K大的数字. 如果想要时间复杂度将为O(log(m+n)).我们可以考虑从K入手.如果我们每次能够删除一个一定在第K个元素之前的元 ...
随机推荐
- OC2-xml文件解析
<?xml version="1.0" encoding="UTF-8"?> <Students class="17班" ...
- js密码的校验(判断字符类型、统计字符类型个数)
/** *判断字符类型 */ function CharMode(iN) { if (iN >= 48 && iN <= 57) //数字 return 1; if (iN ...
- Headfirst设计模式的C++实现——策略模式(Strategy)
前言 最近在学习<Headfirst设计模式>,里面的例子都是Java的.但是我对Java并不熟悉,所以试着用C++来实现书中的例子. 先来看看Duck以及子类 Duck.h #inclu ...
- 正确编写Designated Initializer的几个原则
Designated Initializer(指定初始化器)在Objective-C里面是很重要的概念,但是在日常开发中我们往往会忽视它的重要性,以至于我们写出的代码具有潜藏的Bug,且不易发现.保证 ...
- 《怎样实现通过shell脚本将用户踢出系统》
下面是一个将用户踢出系统的脚本: #!/bin/bashread -p "input your username " userps aux | grep "^$user& ...
- 用canvas 绘制的饼状统计图、柱状统计图、折线统计图
canvas 绘制的饼状统计图 canvas 绘制的柱状统计图 canvas 绘制的折线统计图
- net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net) (转)
作者:王先荣前言 随着Windows操作系统的不断演变,用于捕获视频的API接口也在进化,微软提供了VFW.DirectShow和MediaFoundation这 三代接口.其中VFW早已被Di ...
- linux gd库不支持jpeg解决办法
1. 查看gd库是否支持jpeg gd_info(); 2. 如果JPEG Support 不为1则不支持. 3.首先下载 libjpeg http://www.ijg.org/ ,进行安装 安装目录 ...
- apache ab的安装步骤
1:到apache官方网站http://httpd.apache.org/download.cgi#apache24下载最新版本的apache,然后解压,执行如下命令: ./configure –pr ...
- Noppoo choc mini 84 @XUbuntu13.10 compatibility setting
Months ago, I bought the keyboard Noppoo Choc Mini 84keys for using under XUbuntu12.10, and I have f ...