由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问一道面试题
相关主题
请教大家一道Google的题目一道面试题, 挺难的, 求助
请教一个Axis-Aligned Rectangles的算法微软校园面试总结
求overlap的rectagales怎么求contour of overlapping rectangles
面试遇到扫描线算法和interval tree 问题怎么办问一道flg面试题
问道G题(2)问一道G家面试题
Google onsite interview questionsGoogle interview question
今早的G电面,郁闷坏了...O(NlogN) largest rectangle in histogram
臭名昭著的skyline问题问两个C++的问题
相关话题的讨论汇总
话题: rectangles话题: axis话题: rectangle话题: bst
进入JobHunting版参与讨论
1 (共1页)
d***8
发帖数: 1552
1
题目和解法如下。 没看懂解法。
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.
i****d
发帖数: 35
2
印象中要用到range search
就是去BST里面找的时候
不然分析不出来O(nlgn)的 复杂度

such
corner

【在 d***8 的大作中提到】
: 题目和解法如下。 没看懂解法。
: 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

P********l
发帖数: 452
3
线段重叠题,行程冲突题的升级版。
D*********y
发帖数: 876
4
先把x方向左右两条边都sort一下,O(nlgn)
然后用以下的判断:
bool overlap(Rect a, Rect b) {
return ( a.ul.x <= b.lr.x &&
a.ul.y >= b.lr.y &&
a.lr.x >= b.ul.x &&
a.lr.y <= b.ul.y );
}
其中ul是upper left,lr是lower right
找满足a.ul.x <= b.lr.x和a.lr.x >= b.ul.x的
然后把满足条件的矩形的y方向两条边,加上要比较的矩形本身,sort一下
找到满足a.ul.y >= b.lr.y和a.lr.y <= b.ul.y
1 (共1页)
进入JobHunting版参与讨论
相关主题
问两个C++的问题问道G题(2)
做了一下Kth small in young tablet 和 largest rectangle contain 1sGoogle onsite interview questions
问一道题今早的G电面,郁闷坏了...
问个问题臭名昭著的skyline问题
请教大家一道Google的题目一道面试题, 挺难的, 求助
请教一个Axis-Aligned Rectangles的算法微软校园面试总结
求overlap的rectagales怎么求contour of overlapping rectangles
面试遇到扫描线算法和interval tree 问题怎么办问一道flg面试题
相关话题的讨论汇总
话题: rectangles话题: axis话题: rectangle话题: bst