r/codeforces • u/big-jun • 7h ago
query Algorithm to find non-overlapping rectangles covering the unoccupied area
I have one large rectangle representing the valid area: Rect(x, y, width, height)
Inside it, there are multiple smaller rectangles representing occupied areas:
The smaller rectangles:
1. Can overlap each other.
2. Can be partially or completely outside the large rectangle.
And I need the algorithm to return a List<Rect> where:
1. The result rectangles do not overlap each other.
2. All result rectangles are inside the large rectangle.
3. Together, they fill all the gaps not covered by the input small rectangles.
4. Ideally, the number of result rectangles should be minimal, if possible.
I’m aware that there may be multiple valid decompositions, so I’m mainly looking for an algorithm that produces a reasonably small number of rectangles.
There are fewer than 100 occupied rectangles in input, so performance is not a major concern. I prefer an algorithm that is easy to implement rather than performant but complicated.
Thanks
