10000 Create Check if Grid can be Cut into Sections.py · erjan/coding_exercises@ce61cff · GitHub
[go: up one dir, main page]

Skip to content

Commit ce61cff

Browse files
authored
Create Check if Grid can be Cut into Sections.py
1 parent b27d989 commit ce61cff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
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:
2+
3+
(startx, starty): The bottom-left corner of the rectangle.
4+
(endx, endy): The top-right corner of the rectangle.
5+
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:
6+
7+
Each of the three resulting sections formed by the cuts contains at least one rectangle.
8+
Every rectangle belongs to exactly one section.
9+
Return true if such cuts can be made; otherwise, return false.
10+
11+
-------------------------------------------------------------------
12+
13+
class Solution:
14+
def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool:
15+
def check(intervals):
16+
intervals.sort()
17+
18+
sections = 0
19+
max_end = intervals[0][1]
20+
21+
for start, end in intervals:
22+
if max_end <= start:
23+
sections += 1
24+
max_end = max(max_end, end)
25+
26+
return sections >= 2
27+
28+
x_intervals = [[rect[0], rect[2]] for rect in rectangles]
29+
y_intervals = [[rect[1], rect[3]] for rect in rectangles]
30+
31+
return check(x_intervals) or check(y_intervals)

0 commit comments

Comments
 (0)
0