2014-05-06 11:31

题目链接

原题:

Find the k-th Smallest Element in Two Sorted Arrays. I followed the algorithm from this post, http://leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html
But it does not handle the case where there are duplicates? Does anyone know how to do that? Also, in Java, how should we reduce the size of the arrays? I used the code below, but did not work.

题目:给定长度为n和m的两个有序数组,找出两数组合并后第K小的数,即升序排第K位的数。

解法1:又是这位“Guy”老兄发的题目,连链接都给贴出来了,明摆着告诉别人这题是他从别处copy来的。不得不说,这道题是非常具有区分度的一道简单题/难题。简单的做法,自然是O(K)时间的算法,在归并过程中就可以得出第K小的元素。

代码:

 // http://www.careercup.com/question?id=6283958983589888
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; int findKthSmallest(vector<int> &a, vector<int> &b, int k)
{
int na, nb; na = (int)a.size();
nb = (int)b.size();
if (na == ) {
return b[k];
} else if (nb == ) {
return a[k];
} else if (a[] > b[nb - ]) {
return (k < nb ? b[k] : a[k - nb]);
} else if (b[] > a[na - ]) {
return (k < na ? a[k] : b[k - na]);
} int i, j; i = j = ;
int res = a[i] < b[j] ? a[i++] : b[j++];
while (i + j <= k) {
if (i == na) {
res = b[j++];
} else if (j == nb) {
res = a[i++];
} else {
res = a[i] < b[j] ? a[i++] : b[j++];
}
} return res;
} int main()
{
vector<int> a, b;
int na, nb;
int i; while (scanf("%d%d", &na, &nb) == && (na > && nb > )) {
a.resize(na);
b.resize(nb);
for (i = ; i < na; ++i) {
scanf("%d", &a[i]);
}
for (i = ; i < nb; ++i) {
scanf("%d", &b[i]);
}
while (scanf("%d", &i) == ) {
printf("%d\n", findKthSmallest(a, b, i));
}
a.clear();
b.clear();
} return ;
}

解法2:这个代码不是我独立写出的,在参考了这篇文章的思路以后,模仿写了一版代码。此算法的复杂度应该是O(log(k))的,文章中说是O(log(n) + log(m))。解法中有个比较关键的思想,i + j = k - 1。如果某个a[i]恰好夹在b[j - 1]和b[j]之间,那么a[i]一定排在归并结果的第i + j + 1位,也就是第k位(从1算起)。反过来b[j]夹在a[i - 1]和a[i]之间道理也是一样的。理解这个道理一开始让我费了一番功夫,理解代码里的各种条件表达式则是另一番功夫了。

代码:

 // http://www.careercup.com/question?id=6283958983589888
#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>
using namespace std; int findKthSmallest(vector<int> &a, vector<int> &b, int na, int nb, int ia, int d, int k)
{
int ib = k - - ia; int pre_a = ia == ? INT_MIN : a[ia - ];
int pre_b = ib == ? INT_MIN : b[ib - ];
int cur_a = ia == na ? INT_MAX : a[ia];
int cur_b = ib == nb ? INT_MAX : b[ib]; if (cur_a >= pre_b && cur_a <= cur_b) {
return cur_a;
}
if (cur_b >= pre_a && cur_b <= cur_a) {
return cur_b;
} if (cur_a > cur_b) {
ia = (k - ) - (ia - d) > nb ? (k - ) - nb : ia - d;
} else {
ia = ia + d > na ? na : ia + d;
} return findKthSmallest(a, b, na, nb, ia, (d + ) / , k);
} int main()
{
vector<int> a, b;
int na, nb;
int i;
int ia; while (scanf("%d%d", &na, &nb) == && (na > && nb > )) {
a.resize(na);
b.resize(nb);
for (i = ; i < na; ++i) {
scanf("%d", &a[i]);
}
for (i = ; i < nb; ++i) {
scanf("%d", &b[i]);
}
while (scanf("%d", &i) == ) {
ia = min(na, i - );
printf("%d\n", findKthSmallest(a, b, na, nb, ia, (ia + ) / , i));
}
a.clear();
b.clear();
} return ;
}

Careercup - Google面试题 - 6283958983589888的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. .NET中使用log4net

    一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...

  2. 开源自己的一个小android项目(美女撕衣服游戏)

    这是自己的一个开源自己的一个小android项目(美女撕衣服游戏),也是前6个月开发的,有部分的资源来自网络上的,现在开源出来给大家吧,由于源码比较大,不上传了,我已经上传到源码天堂那个网站那里了,大 ...

  3. Raspberry Pi B+ 定时向物联网yeelink上传CPU GPU温度

     Raspberry Pi B+ 定时向物联网yeelink上传CPU GPU温度 硬件平台: Raspberry Pi B+ 软件平台: Raspberry 系统与前期安装请参见:树莓派(Ros ...

  4. VS2010在非IE浏览器下调试Silverlight程序

    以Chrome为例: 第一步:在程序中设置断点. 第二步:右键点击web应用程序的起始页(.html或.aspx文件),选择"浏览方式",选中Chrome或其它非IE浏览器,点&q ...

  5. 文本分析工具awk简单示例

    先创建一个文件:vim hi 取第2个字段和第3个字段: awk '{print $2,$3}' hi     注意{}中的,逗号会在输出的时候转变为空格 加入字符说明: 显示整行: 指定字段分隔符: ...

  6. mac下,利用homebrew安装PHP+Mysql+Nginx

    具体可以参考直通车 http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-home ...

  7. php数组去重复代码

    php数组去重复数据示例,有时候获得的php数组中总是出现value重复的,使用下面的方法就可以去掉重复数据 以数字开头的重复数据如: Array (  [0] => 100  [k1] =&g ...

  8. jQuery学习笔记(5)--表单域获得焦点和失去焦点样式变化

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. 第十一章 管理类型(In .net4.5) 之 管理对象的生命周期

    1. 概述 本章内容包括 管理非托管资源.使用IDisposable接口 以及 管理析构器和垃圾回收. 2. 主要内容 2.1 理解垃圾回收机制 ① 代码执行的时候,内存中有两个地方存放数据项:堆 和 ...

  10. 关于datagridview的一些操作

    1.绑定datatable时,会显示出不需要显示的列可以加datagridview.AutoGenerateColumns = false; 2.如果datagridview的某列是数值型的,有小数, ...