Given a set of non-overlapping rectangles on the plane, and two real numbers , find a rectangle of of width and height , maximizing .
This is an interview question.
The first observation is that the upper side must coincide with the upper edge of some and the left side must coincide with the left edge of some rectangle in . With this observation, we can already get an algorithm of time complexity : enumerate over all possible choices of upper side and left side of , note that once the upper side and left side of is fixed, the rectangle is fixed (as we know its width and height). There are candidates for the upper side, and candidates for the left side, so there are candidates of . For each candidate, we compute : this can be done in time, as for each rectangle, we can determine in constant time the area of overlapping region of and . So the overall time complexity is .
But I feel that there should be faster algorithms. There are several reasons:
(1) It feels quite inefficient to examine all rectangles for each of the candidate rectangle. Maybe we could use the sweep-line algorithm: first sort the candidate of upper sides in time , then sweep a horizontal slab of height from north to south, where the state is the set of rectangles in overlapping the slap. For each slab, we again sweep the candidate rectangle from west to east, enumerating over all possible left sides of rectangles intersected by the current slab.
(2) Using interval trees, one can query the rectangles intersected by a rectangle in time , where is the number of rectangles contained in the query rectangle.
(3) Perhaps we could also use other space partition data structures to quickly compute the overlapping area of a candidate rectangle.
My guess is that in the worst case, time complexity algorithm should be possible, but I hope there would be a time complexity algorithm….