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. Ubuntu 14.04 – How to install xrdp in Ubuntu 14.04

    http://c-nergy.be/blog/?p=5305 Hello World, Ubuntu 14.04 has been released on April 17th 2014 and we ...

  2. Hadoop安装(Ubuntu Kylin 14.04)

    安装环境:ubuntu kylin 14.04   haoop-1.2.1   hadoop下载地址:http://apache.mesi.com.ar/hadoop/common/hadoop-1. ...

  3. java项目编译有误

    classpath component

  4. 最简洁粗暴版的虚拟用户配置FTP

    最简洁粗暴版的虚拟用户配置FTP yum安装FTP: yum install vsftpd pam* db4* -y 设置为系统服务:chkconfig –level 35 vsftpd on 2.v ...

  5. centos6.7下网络设置

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static"   # ...

  6. int 类型 占多少字节是由什么决定的

    int 类型占据多少字节?到底是跟编译器有关?还是系统来决定的? 1. CPU的设计者才不管你在上面跑什么程序.他们只是按着他们的想法来设计.而int的大小,至少在C/C++中,标准只说可以由实现者自 ...

  7. 13)Java static

    1.static变量      按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是没有被static修饰的变量,叫实例变量.两者的区别是:    ...

  8. ASP.NET MVC5学习笔记之Controller同步执行架构分析

    在开始之前,声明一下,由于ASP.NET MVC5正式发布了,后面的分析将基于ASP.NET MVC5最新的源代码.在前面的内容我们分析了怎样根据路由信息来确定Controller的类型,并最终生成C ...

  9. delphi构造&析构调用顺序

    _ClassCreate ->Create ->AfterConstruction(->DoCreate / OnCreate) BeforeDestruction(->DoD ...

  10. 第六章Linux的文件权限与目录配置

    一.Linux用户分类 1.Linux用户分为:(文件|目录)所有者(OWN),(同组内的)用户组,其他人; 2.一个天神:root;,几乎能完成任何事.... 二.目录权限的意义 目录的权限和文件的 ...