-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck if Grid can be Cut into Sections.py
31 lines (22 loc) · 1.48 KB
/
Check if Grid can be Cut into Sections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.
-------------------------------------------------------------------
class Solution:
def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool:
def check(intervals):
intervals.sort()
sections = 0
max_end = intervals[0][1]
for start, end in intervals:
if max_end <= start:
sections += 1
max_end = max(max_end, end)
return sections >= 2
x_intervals = [[rect[0], rect[2]] for rect in rectangles]
y_intervals = [[rect[1], rect[3]] for rect in rectangles]
return check(x_intervals) or check(y_intervals)