You are given an integer n representing the dimensions of an n x n grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where rectangles[i] is in the form [startx, starty, endx, endy], representing a rectangle on the grid. Each rectangle is defined as follows:
(startx, starty): The bottom-left corner of the rectangle.
(endx, endy): The top-right corner of the rectangle.
Note that the rectangles do not overlap. Your task is to determine if it is possible to make either two horizontal or two vertical cuts on the grid such that:
Each of the three resulting sections formed by the cuts contains at least one rectangle.
Every rectangle belongs to exactly one section.
Return true if such cuts can be made; otherwise, return false.
We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.
Constraints:
3 <= n <= 109
3 <= rectangles.length <= 105
0 <= rectangles[i][0] < rectangles[i][2] <= n
0 <= rectangles[i][1] < rectangles[i][3] <= n
No two rectangles overlap.
Solutions
Solution 1
Thinking
Rectangles do not overlap; we ask whether two axis-aligned cuts can split them into three nonempty parts. Coordinates reach \(10^9\), so we work on projections.
Each interval contributes a start \(+1\) and an end \(-1\). Equal coordinates process ends first so a touch creates a gap.
Whenever coverage returns to \(0\) we have a full seam. Either orientation with at least three seams (two cuts) is valid.
classSolution:defcountLineIntersections(self,coordinates:List[tuple[int,int]])->bool:lines=0overlap=0forvalue,markerincoordinates:ifmarker==0:overlap-=1else:overlap+=1ifoverlap==0:lines+=1returnlines>=3defcheckValidCuts(self,n:int,rectangles:List[List[int]])->bool:y_coordinates=[]x_coordinates=[]forrectinrectangles:x1,y1,x2,y2=recty_coordinates.append((y1,1))# starty_coordinates.append((y2,0))# endx_coordinates.append((x1,1))# startx_coordinates.append((x2,0))# end# Sort by coordinate value, and for tie, put end (0) before start (1)y_coordinates.sort(key=lambdax:(x[0],x[1]))x_coordinates.sort(key=lambdax:(x[0],x[1]))returnself.countLineIntersections(y_coordinates)orself.countLineIntersections(x_coordinates)
classSolution{#define pii pair<int, int>boolcountLineIntersections(vector<pii>&coordinates){intlines=0;intoverlap=0;for(inti=0;i<coordinates.size();++i){if(coordinates[i].second==0)overlap--;elseoverlap++;if(overlap==0)lines++;}returnlines>=3;}public:boolcheckValidCuts(intn,vector<vector<int>>&rectangles){vector<pii>y_cordinates,x_cordinates;for(auto&rectangle:rectangles){y_cordinates.push_back(make_pair(rectangle[1],1));y_cordinates.push_back(make_pair(rectangle[3],0));x_cordinates.push_back(make_pair(rectangle[0],1));x_cordinates.push_back(make_pair(rectangle[2],0));}sort(y_cordinates.begin(),y_cordinates.end());sort(x_cordinates.begin(),x_cordinates.end());// Line-Sweep on x and y cordinatesreturn(countLineIntersections(y_cordinates)orcountLineIntersections(x_cordinates));}};