由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Find the K largest element in a sorted M*N array
相关主题
请教一个老算法题, k-th largest sum问个算法题8
一道热门的 Google 面试题heap sort的缺点是什么?和quick sort比
一个NxN矩阵每行每列都sort好,如何排序?贡献另外一个Amazon面试的题
find Kth Largest Element 有没有更简化的解法One Microsoft interview question
Kth Largest Element in an Array算法复杂度问一道F家面试题
问道算法题Print out all elements in a sorted matrix
Find the Kth smallest element in 2 sorted问个google面试题
算法一问一个特别的inplace merge two sorted arrays
相关话题的讨论汇总
话题: find话题: sorted话题: heap话题: largest话题: max
进入JobHunting版参与讨论
1 (共1页)
m**q
发帖数: 189
1
Looks like an old question, but failed to find a better solution than O(klg(
m+n)). Any ideas?
The question is quoted from a comment on ihas1337code website
------
1.Two reverse sorted arrays A and B have been given. such that size of A is
m and size of B is n
You need to find the k th largest sum (a+b) where a is taken from A and b is
taken from B. such that k < m*n It can be done in O(n*n) but i believe it
can be done in a very efficient way. I heard someone say there is a paper
from SODA. It's possible to solve it in O(N), however, it requires very
complex algorithm. I am wondering if you can come up with an NlogN algorithm
first then try O(N). These two problems have been very hot interview
problems from Google, however, not many people can solve it very efficiently
yet. I believe you can do it after I read many posts on your site.
2. Given a N*N Matrix. All rows are sorted, and all columns are sorted. Find
the Kth Largest element of the matrix.
e******s
发帖数: 38
2
1. 第一个题好像是用一个max-heap做的,就是每次从heap里pop出一个
pair(a[i]+b[j]),然后将 pair(a[i],b[j+1]) 和pair(a[i+1],b[j]) insert到heap里。
但好像需要check是不是重复的pair(a[i],b[j])已经在heap里了。
为避免重复,必须用 个变量J-Max track 目前max-heap中所有pair的最大的j. 如果j
< J_Max, insertpair(a[i+1],b[j])到heap里; 如果j = J_Max, 将 pair(a[i],b[j+1
]) 和pair(a[i+1
],b[j]) insert到heap里, update as J_Max = J_Max+1.
原帖在 http://www.mitbbs.com/article_t0/JobHunting/31840297.html
2.第二个题,用一个best first search, 从二维数组左上角开始search.
maintain 一个priority queue (= min_heap 来实现), heap.pop()返回heap的min
element.
(1)初始的时候,将M[0][0]insert到min_heap里,
(2)for (i = 0; i < k-1; i++) {
tmp = heap.pop(); // 假设 tmp 为 m[i][j]
if m[i][j+1]没有被insert到heap 过, heap.insert(m[i][j+1]);
if m[i+1][j]没有被insert到heap 过, heap.insert(m[i+1][j]);
}
return(heap.pop());
思路就是: 二维数组左上方的元素最小,然后从左上方开始做best first search, 每
次heap pop out出来的元素必然是递增的。 执行操作k-1次之后,第k个从heap里pop
out出来的就是第k大的元素。 复杂度 O(klog(k)). 不知道有没有更好的解法。

klg(
is
is
it

【在 m**q 的大作中提到】
: Looks like an old question, but failed to find a better solution than O(klg(
: m+n)). Any ideas?
: The question is quoted from a comment on ihas1337code website
: ------
: 1.Two reverse sorted arrays A and B have been given. such that size of A is
: m and size of B is n
: You need to find the k th largest sum (a+b) where a is taken from A and b is
: taken from B. such that k < m*n It can be done in O(n*n) but i believe it
: can be done in a very efficient way. I heard someone say there is a paper
: from SODA. It's possible to solve it in O(N), however, it requires very

g*****i
发帖数: 2162
3
这个J-Max track array思路很好,但是表现上比hashset会好很多吗?
第二题判断有没有插入过似乎这个J-max就用不上了.

里。
j
+1

【在 e******s 的大作中提到】
: 1. 第一个题好像是用一个max-heap做的,就是每次从heap里pop出一个
: pair(a[i]+b[j]),然后将 pair(a[i],b[j+1]) 和pair(a[i+1],b[j]) insert到heap里。
: 但好像需要check是不是重复的pair(a[i],b[j])已经在heap里了。
: 为避免重复,必须用 个变量J-Max track 目前max-heap中所有pair的最大的j. 如果j
: < J_Max, insertpair(a[i+1],b[j])到heap里; 如果j = J_Max, 将 pair(a[i],b[j+1
: ]) 和pair(a[i+1
: ],b[j]) insert到heap里, update as J_Max = J_Max+1.
: 原帖在 http://www.mitbbs.com/article_t0/JobHunting/31840297.html
: 2.第二个题,用一个best first search, 从二维数组左上角开始search.
: maintain 一个priority queue (= min_heap 来实现), heap.pop()返回heap的min

1 (共1页)
进入JobHunting版参与讨论
相关主题
一个特别的inplace merge two sorted arraysKth Largest Element in an Array算法复杂度
真慫阿, Facebook 1st phone interview,问道算法题
The time complexity on finding the kth largest element in aFind the Kth smallest element in 2 sorted
数组中找和为0的3个数,4个数算法一问
请教一个老算法题, k-th largest sum问个算法题8
一道热门的 Google 面试题heap sort的缺点是什么?和quick sort比
一个NxN矩阵每行每列都sort好,如何排序?贡献另外一个Amazon面试的题
find Kth Largest Element 有没有更简化的解法One Microsoft interview question
相关话题的讨论汇总
话题: find话题: sorted话题: heap话题: largest话题: max