Careercup - Google面试题 - 6283958983589888
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的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- 对C语言中va_list,va_start,va_arg和va_end的一点理解
这几个函数和变量是针对可变参数函数的,什么是可变参数函数呢,最经典的莫过于printf和scanf,这两个函数的声明如下: int printf(const char *format, ...); i ...
- jQuery下拉友情链接美化效果代码分享
这篇文章主要介绍了jQuery下拉友情链接美化效果,很实用的代码,推荐给大家,有需要的小伙伴可以参考下. 文实例讲述了jQuery下拉友情链接美化效果,jQuery下拉友情链接美化代码是一款基于jQu ...
- linux中的audit审计日志
这里首先介绍auditctl的应用,具体使用指南查看man auditctl.auditctl的man 描述说明这个工具主要是用来控制audit系统行为,获取audit系统状态,添加或者删除audit ...
- 后缀为inc的是什么文件?C#中如何包含inc文件?
在项目Web页面文件中,发现这么一句话: <!-- 页面字符集设置 begin--><!-- #INCLUDE FILE="http://www.cnblogs.com/C ...
- jquery.pagination +JSON 动态无刷新分页
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlPage.aspx.cs& ...
- SQL Server编程(05)游标
在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...
- Uva12504 Updating a Dictonary
这道题难度不大,主要是考察熟练运用C++的容器,字符串等操作. 另外注意特殊情况是否需要特殊处理.即当一个字典为空时,无论另一个字典是否有值,输出的结果都为No Change,这点需要注意一下. 另外 ...
- nginx报file not found错误
查看错误日志 [:q2013/10/20 18:41:40 [error] 27151#0: *106117 FastCGI sent in stderr: "Primary script ...
- 在PyQt中直接使用ui文件并加载qrc资源文件
1. 用Qt设计师创建一个包含qrc资源文件的ui文件 2.打开cmd使用以下命令把qrc资源文件转换成十六进制的py文件 pyrcc4 -o C:\res.py C:\res.qrc pyrcc4 ...
- 在xml中添加array
在values建立arrays(名字可自定义)的xml: <?xml version="1.0" encoding="utf-8"?> <re ...