You are given a list of rectangles, where each rectangle is represented as a pair [length, width]
. A rectangle can form a square if and only if both its length and width are at least as large as the side of the square. Your task is to determine how many rectangles from the list can form the largest possible square (i.e., the square with the maximum possible side length that can be formed from any rectangle in the list).
length
and width
.[[5,8],[3,9],[5,12],[16,5]]
.
At first glance, you might think you need to consider all possible square sizes for each rectangle. However, since we only care about the largest square that can be formed, we can simplify the problem. For each rectangle, the largest square it can form is determined by the smaller of its length and width. So, for each rectangle, we record side = min(length, width)
. Our goal is then to find the maximum value of side
across all rectangles, and count how many rectangles have this value.
This leads to a two-step approach:
Let's break down the solution into clear steps:
min(length, width)
.To implement this, you can use a single loop:
max_side
to 0 and count
to 0.side = min(length, width)
.side > max_side
, set max_side = side
and reset count = 1
.side == max_side
, increment count
by 1.count
holds the answer.This method is efficient, requiring only a single pass through the rectangles.
Let's walk through the example input [[5,8],[3,9],[5,12],[16,5]]
:
[5,8]
→ min(5,8) = 5
[3,9]
→ min(3,9) = 3
[5,12]
→ min(5,12) = 5
[16,5]
→ min(16,5) = 5
The possible square sides are: [5, 3, 5, 5]
. The largest is 5
. There are 3
rectangles that can form a square of side 5
(the first, third, and fourth rectangles).
Thus, the answer is 3.
class Solution:
def countGoodRectangles(self, rectangles):
max_side = 0
count = 0
for l, w in rectangles:
side = min(l, w)
if side > max_side:
max_side = side
count = 1
elif side == max_side:
count += 1
return count
class Solution {
public:
int countGoodRectangles(vector<vector<int>>& rectangles) {
int max_side = 0, count = 0;
for (const auto& rect : rectangles) {
int side = min(rect[0], rect[1]);
if (side > max_side) {
max_side = side;
count = 1;
} else if (side == max_side) {
count++;
}
}
return count;
}
};
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int maxSide = 0, count = 0;
for (int[] rect : rectangles) {
int side = Math.min(rect[0], rect[1]);
if (side > maxSide) {
maxSide = side;
count = 1;
} else if (side == maxSide) {
count++;
}
}
return count;
}
}
var countGoodRectangles = function(rectangles) {
let maxSide = 0, count = 0;
for (let rect of rectangles) {
let side = Math.min(rect[0], rect[1]);
if (side > maxSide) {
maxSide = side;
count = 1;
} else if (side === maxSide) {
count++;
}
}
return count;
};
O(n)
since you only need to check min(length, width)
for each rectangle. No nested loops are needed.
O(n)
, where n
is the number of rectangles.
O(1)
.
The key insight is that for each rectangle, the largest square it can form is determined by its smallest side. By tracking the maximum possible square side and counting how many rectangles achieve this, we reduce the problem to a simple pass through the list. This approach is both elegant and efficient, making it ideal for both interviews and production code.