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 ...
随机推荐
- UIButton设置imgae图片自适应button的大小且不变形
在某些情况下,我们使用的UIButton的背景图片不一定就是标准的尺寸,有时会偏大,那么怎么办? 这个比较简单直接设置 : self.imageView.contentMode = UIView ...
- [原]Python 简单异常处理
s=raw_input("Input your age:") if s =="": raise Exception("Input must no be ...
- POJ C++程序设计 编程题#1 大整数的加减乘除
编程题#4:大整数的加减乘除 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 ...
- Linux下CPU占用率高分析方法
一般解决方法是通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用1. 通过top命令找到可疑进程PID top - 09:37:18 up 70 days, 16:29 ...
- 请注意designer中的NAME field
If you are trying to set the value of a field through PeopleCode and that field happens to be NAME t ...
- css3动画响应式404页面
PC端效果: 模拟触屏端效果: 兼容性:触屏端及桌面端(优雅降级至IE6) 模板下载: http://pan.baidu.com/s/1o67ftc2
- struts2 s:if标签以及 #,%{},%{#}的使用方法等在资料整理
<s:if>判断字符串的问题: 1.判断单个字符:<s:if test="#session.user.username=='c'"> 这样是从session ...
- opengl基础学习专题 (二) 点直线和多边形
题外话 随着学习的增长,越来越觉得自己很水.关于上一篇博文中推荐用一个 学习opengl的 基于VS2015的 simplec框架.存在 一些问题. 1.这个框架基于VS 的Debug 模式下,没有考 ...
- Unix 编程
1. Users and Groups 真实用户ID和真实组ID 真实用户ID和组ID表示运行进程的真实用户 ID 和 组ID. 有效用户ID和有效组IDp 有效 ID 是进程进行相关操作(比如系统调 ...
- Ubuntu下部署java JDK和eclipse IDE
安装Java编程开发环境: Ubuntu默认安装openjava,可以通过java -version查看是否安装.但我使用Ubuntu9.10升级到10.04LTS时,openjava没有了.另外,如 ...