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. java编程的78条黄金法则

    创建和销毁对象 1.考虑用静态工厂方法(返回类的实例的静态方法)代替构造器2.遇到多个构造器参数时要考虑用构造器3.用私有构造器或者枚举类型强化Singleton属性4.通过私有构造器强化不可实例化的 ...

  2. (二)、NodeJS 、Express4安装使用方法

    第一步:安装Nodejs 第二步:安装express等部件 1.打开命令窗口,安装express.jade npm install -g express npm install -g express- ...

  3. 显示或隐藏一个Grid

    The Rowset class contains two methods that can be used to show and hide all rows: ShowAllRows() Hide ...

  4. jQuery在IE7和8下setInterval失效的问题

    原因不在于setInterval,而是IE的缓存造成ajax请求页没有更新的问题. 在请求的url中加入一个随机数参数即可. var CheckPaied = function (transactio ...

  5. azure注册码

    用户名:aaa 注册秘钥:2GQrt5XHYY7SBK/4b22Gm4Dh8alaR0/0k3gEN5h7FkVPIn8oG3uphlOeytIajx 注册用户名:www.yuanxingku.com ...

  6. ajax翻页效果模仿yii框架

    ajax翻页效果,模仿yii框架. 复制代码代码如下: <!DOCTYPE html>  <html>  <head>  <title>ajax分页_w ...

  7. JS中cookie的基本使用

    cookie是本身是HTML中ducument中的一个属性,可以用来保存一些简单的数据信息,比如用户名.密码等,提高一些网站的用户体验度.下面就来简单的说说cookie,它有下面几个特性: 1.有过期 ...

  8. ArrayAdapter的简单使用

    1.创建一个类继承ArrayAdapter private class MyAdapter extends ArrayAdapter { LayoutInflater in; Context cont ...

  9. delphi 连接MYSQL 的方法

    需要的控件:(view as form) 1.SQLConnection1: TSQLConnection ConnectionName = 'MYSQLCONNECTION' DriverName ...

  10. [.NET 4.5] ADO.NET / ASP.NET 使用 Async 和 Await 异步 存取数据库

    此为文章备份,原文出处(我的网站)  [.NET 4.5] ADO.NET / ASP.NET 使用 Async 和 Await 异步 存取数据库 http://www.dotblogs.com.tw ...