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:
Return true
if such cuts can be made; otherwise, return false
.
For example:
n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]
. The output is true
because we can make horizontal cuts at y = 2
and y = 4
.n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]
. The output is true
because we can make vertical cuts at x = 2
and x = 3
.n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]
. The output is false
because we cannot make two horizontal or two vertical cuts that satisfy the conditions.def solve():
n = int(input())
rectangles_str = input()
rectangles = []
rects = rectangles_str.strip('[]').split('],[')
for rect_str in rects:
rect = list(map(int, rect_str.strip('[]').split(',')))
rectangles.append(rect)
def check_horizontal_cuts(cut1, cut2):
if not (0 < cut1 < n and 0 < cut2 < n and cut1 < cut2):
return False
sections = [[] for _ in range(3)]
for rect in rectangles:
if rect[1] < cut1 and rect[3] <= cut1:
sections[0].append(rect)
elif rect[1] >= cut1 and rect[3] <= cut2:
sections[1].append(rect)
elif rect[1] >= cut2 and rect[3] <= n:
sections[2].append(rect)
else:
return False
return all(len(section) > 0 for section in sections)
def check_vertical_cuts(cut1, cut2):
if not (0 < cut1 < n and 0 < cut2 < n and cut1 < cut2):
return False
sections = [[] for _ in range(3)]
for rect in rectangles:
if rect[0] < cut1 and rect[2] <= cut1:
sections[0].append(rect)
elif rect[0] >= cut1 and rect[2] <= cut2:
sections[1].append(rect)
elif rect[0] >= cut2 and rect[2] <= n:
sections[2].append(rect)
else:
return False
return all(len(section) > 0 for section in sections)
for i in range(1, n):
for j in range(i + 1, n):
if check_horizontal_cuts(i, j) or check_vertical_cuts(i, j):
print("true")
return
print("false")
# Example Usage (replace with actual input):
# n = 5
# rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]
# n = 4
# rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]
# n = 4
# rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]
# To run with input:
# solve()
# Example 1
n = 5
rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]
def check_solution(n, rectangles):
def check_horizontal_cuts(cut1, cut2):
if not (0 < cut1 < n and 0 < cut2 < n and cut1 < cut2):
return False
sections = [[] for _ in range(3)]
for rect in rectangles:
if rect[1] < cut1 and rect[3] <= cut1:
sections[0].append(rect)
elif rect[1] >= cut1 and rect[3] <= cut2:
sections[1].append(rect)
elif rect[1] >= cut2 and rect[3] <= n:
sections[2].append(rect)
else:
return False
return all(len(section) > 0 for section in sections)
def check_vertical_cuts(cut1, cut2):
if not (0 < cut1 < n and 0 < cut2 < n and cut1 < cut2):
return False
sections = [[] for _ in range(3)]
for rect in rectangles:
if rect[0] < cut1 and rect[2] <= cut1:
sections[0].append(rect)
elif rect[0] >= cut1 and rect[2] <= cut2:
sections[1].append(rect)
elif rect[0] >= cut2 and rect[2] <= n:
sections[2].append(rect)
else:
return False
return all(len(section) > 0 for section in sections)
for i in range(1, n):
for j in range(i + 1, n):
if check_horizontal_cuts(i, j) or check_vertical_cuts(i, j):
return True
return False
print(check_solution(n, rectangles))
# Example 2
n = 4
rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]
print(check_solution(n, rectangles))
# Example 3
n = 4
rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]
print(check_solution(n, rectangles))
check_horizontal_cuts(cut1, cut2)
and check_vertical_cuts(cut1, cut2)
functions: These functions take two potential cut positions as input and determine if those cuts are valid. They return True
if the cuts are valid (each section contains at least one rectangle and all rectangles are within the bounds), and False
otherwise.
Iterating through possible cuts: The main part of the solution iterates through all possible pairs of distinct cut positions (i, j)
where 1 <= i < j < n
. For each pair, it checks if horizontal or vertical cuts at these positions satisfy the given conditions.
Early exit: If a valid pair of cuts is found (either horizontal or vertical), the function immediately returns True
. If no such pair is found after checking all possibilities, it returns False
.
The time complexity of the solution is O(n^2 * r), where n is the dimension of the grid and r is the number of rectangles. This comes from the nested loops that iterate through all possible pairs of cut positions (O(n^2)) and, within each pair, iterating through all rectangles to check if they fall within the defined sections (O(r)).
The space complexity is O(r) in the worst case. This space complexity is due to the creation of the sections
list in the check_horizontal_cuts
and check_vertical_cuts
functions. In the worst case, these lists may contain all the rectangles if they all fall into one of the sections. The other variables used take constant space relative to the input size.