You are given an array of rectangles
where rectangles[i] = [li, wi]
represents the dimensions of the i-th
rectangle on a flat plane.
You can rotate the rectangle any number of times, i.e., swap its width and height. Your task is to determine the largest square side length that can be obtained from the given rectangles and count how many rectangles can make such a square.
Return the number of rectangles that can make the largest square.
n
, can be up to 1000.[li, wi]
, the largest side length of a square that can be formed is min(li, wi)
.def countGoodRectangles(rectangles):
max_side_length = 0
count = 0
for l, w in rectangles:
square_side = min(l, w)
if square_side > max_side_length:
max_side_length = square_side
count = 1
elif square_side == max_side_length:
count += 1
return count
# Example usage:
rectangles = [[5,8], [3,9], [5,12], [16,5]]
print(countGoodRectangles(rectangles)) # Output: 3
n
is the number of rectangles. We iterate through the list of rectangles once to determine the largest possible square side length and count how many rectangles can form it.min(l, w)
).Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?