The First Six Months Developing For The Computer On My Face
-
[image: JonGottfried]*Editor's note: **Jon Gottfried is a Developer
Evangelist at Twilio, co-founder of the Hacker Union, and a StartupBus
Conductor. Being...
13 minutes ago
I like this problem since it is a classical example of simple computational geometry. A solution is like the following. Sort the x-coordinates of the rectangles in time O(nlogn), and maintains the y-coordinates in a binary search tree (BST). If the current x-coordinate is on the left side of the rectangle, then insert in the BST the corresponding y-coordinates. If the current x-coordinate is on the right side of the rectangle, then remove the corresponding y-coordinates from the BST. Before inserting, search the BST to find an overlap with current y-coordinate. There are 2n search and each one takes O(logn). The total complexity is O(nlogn)
ReplyDelete