Not Sew difficult
Jump to navigation
Jump to search
Classic interesting points problem. The grid is huge, but we only care about points where rectangles start or stop...which is only 2000 points in each dimension.
- add all the x coordinates to a list and sort it. The index in this list is what we replace the original indices by
- repeat for y
- at this point, our grid is only 2000x2000!...easily small enough to process the whole thing
- do a scan line starting at x=0, noting which y indices have how many rectangles in them
- increment the scanline to x=1 and beyond, each time walking the y values and adding 1 to indices where a new patch starts, or subtracting if it goes away
- return whatever the max was found during the sweep.
The astute may find that we actually only need to compress the Y dimension, since the sweep line only has to stop at the interesting points. In any case, the runtime is 4,000,000, since we process every spot on the grid exactly once.
There may be ways to do this faster using an interval tree instead of a flat index for the scan.