Problem Description
Given an n x n grid and a list of non‐overlapping rectangles (each defined by its bottom‐left and top‐right coordinates), determine whether it is possible to make either two horizontal cuts or two vertical cuts (i.e. full-length lines across the grid) so that the grid is partitioned into three sections where: • Each section contains at least one rectangle. • Each rectangle lies completely in exactly one section (i.e. no cut goes through any rectangle).
Key Insights
- Since a cut is a full line, it may only be placed on coordinates that are exactly a rectangle’s boundary (start or end) to avoid “cutting” a rectangle.
- A valid horizontal (or vertical) cut at coordinate c is safe if for every rectangle either the rectangle’s top is ≤ c or its bottom is ≥ c.
- Because the cut must separate the rectangles completely, one must partition the rectangles into three nonempty groups such that all rectangles in the lower group lie entirely below the first cut, all rectangles in the middle group lie completely between the two cuts, and all rectangles in the upper group lie completely above the second cut.
- The counts of rectangles that lie “entirely below” a candidate cut (using top boundaries) and “entirely above” a candidate cut (using bottom boundaries) are monotonic when the candidate cut is taken from the sorted unique boundaries.
- The problem has a symmetric “horizontal” and “vertical” version. If either possibility yields a valid pair of cuts – two horizontal or two vertical – then the answer is true.
Space and Time Complexity
Time Complexity: O(m log m) per orientation, where m is the number of unique boundaries (at most 2 · (rectangles.length)). Overall O(m log m) since the two orientations are handled similarly. Space Complexity: O(m) for storing the candidate cut positions and auxiliary arrays.
Solution
The solution considers both horizontal and vertical cutting possibilities. For a fixed orientation (say horizontal), we first extract the y‐coordinates that are “candidate” cut lines (all rectangle start_y and end_y values). Using two sorted arrays – one for rectangle top boundaries (end_y) and one for bottom boundaries (start_y) – we can for any candidate coordinate c quickly determine: • group1_count: number of rectangles completely below or touching c (i.e. with top ≤ c) • group3_count: number of rectangles completely above or touching c (i.e. with bottom ≥ c) A valid pair of candidate cuts (L, R with L < R) satisfies: – group1_count(L) ≥ 1 (at least one rectangle is completely below L), – group3_count(R) ≥ 1 (at least one rectangle is completely above R), and – The “middle” group count (which is total – group1_count(L) – group3_count(R)) is at least 1. Because the counts are monotonic over the candidate boundaries, we can iterate over possible L and use a binary search (or two‑pointer technique) to find a corresponding valid R. We then repeat the same process for vertical cuts (using x boundaries). If either horizontal or vertical partitioning works, we return true.