由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一个Axis-Aligned Rectangles的算法
相关主题
请教大家一道Google的题目怎么求contour of overlapping rectangles
请问一道面试题Google onsite interview questions
求overlap的rectagales问一个算法题
面试遇到扫描线算法和interval tree 问题怎么办这题咋做?
问道G题(2)一道面试题, 挺难的, 求助
臭名昭著的skyline问题求问twitter电面
大家来讨论一下这个求长方形面积的问题吧问个google面经题
微软校园面试总结算法--一个MXN matrix (0's and 1's)内求最大 rectangle(1's)
相关话题的讨论汇总
话题: rectangles话题: axis话题: bst话题: aligned话题: rectangle
进入JobHunting版参与讨论
1 (共1页)
j***y
发帖数: 2074
1
还在看Hacking a Google Interview的材料,正看到下面这段:
Question: Axis-Aligned Rectangles
Describe an algorithm that takes an unsorted array of axis‐aligned
rectangles and
returns any pair of rectangles that overlaps, if there is such a pair. Axis-
aligned means that all the rectangle sides are either parallel or
perpendicular to the x‐ and y‐axis. You can assume that each rectangle
object has two variables in it: the x‐y coordinates of the upper‐left
corner and the bottom‐right corner.
Good Answer: Create a sorted array of the x coordinates of the left and
right edges of the rectangles. Then, use a "scanline" to move from left to
right through the rectangles. Keep a binary search tree containing the y
coordinates of the top and bottom edges of the rectangles that overlap the
scanline. For each element of the array, check whether it is a left or right
edge. If it is a right edge, remove the corresponding top and bottom edges
from the BST. If it is a left edge, search the BST for rectangles that
overlap the current rectangle; if there is one, return the overlap. Then,
add the y coordinates of the top and bottom edges of the rectangle to the
BST. The search takes O(n log n) time, since it takes O(n log n) time to
sort the rectangles and each of the 2n iterations takes O(log n) time.
翻来覆去看了很多遍,可是还是没怎么看懂。为什么scanline是矩形右边界的时候要
remove,是左边界的时候要insert啊?还有这个“search the BST for rectangles
that overlap the current rectangle”具体是怎么样操作的啊?
大牛们能不能给详细说一下?或给点参考文献的link?
谢了先。
b*******8
发帖数: 37364
2
从左到右扫描,碰到左边界,表示开始进入这个矩形,碰到右边界,表示扫描出了这个
矩形。任一时刻,同时在BST里的矩形,X轴上一定Overlap,但还要在Y轴上进行判断是
否Overlap。用BST是为了搜索,插入,删除的时间复杂度都是log级别。
j***y
发帖数: 2074
3
谢谢,经你这么解释一下清楚多了。不过,代码写起来对我还是不太容易。
可以用C或者C++给个例子吗?
b*******8
发帖数: 37364
4
如果要现场Coding,涉及到BST的操作可以用伪函数代替吧,如果是平衡BST那就更是如
此。其他部分并不复杂。要展开写平衡BST的构造维护,那就是一个专门的面试题目了
a***r
发帖数: 93
5
I still have no clue how the solution works, could someone give a hint,
better with some simple code.
Thanks:)
1 (共1页)
进入JobHunting版参与讨论
相关主题
算法--一个MXN matrix (0's and 1's)内求最大 rectangle(1's)问道G题(2)
leetcode container with most water臭名昭著的skyline问题
Maximal Rectangle如果不要求是Rectangle就要简单得多大家来讨论一下这个求长方形面积的问题吧
Smallest Rectangle Enclosing Black Pixels微软校园面试总结
请教大家一道Google的题目怎么求contour of overlapping rectangles
请问一道面试题Google onsite interview questions
求overlap的rectagales问一个算法题
面试遇到扫描线算法和interval tree 问题怎么办这题咋做?
相关话题的讨论汇总
话题: rectangles话题: axis话题: bst话题: aligned话题: rectangle