Problem Description
Given an array of rectangles where each rectangle is represented by its length and width, determine the number of rectangles that can form the largest square. The side length of the square that can be cut out of a rectangle is limited by the smaller of the rectangle’s two dimensions. First, find the maximum square side length possible from all rectangles, then count how many rectangles can produce a square of that side length.
Key Insights
- The largest square from a rectangle is determined by the minimum of its two dimensions.
- The maximum possible square side (maxLen) is the largest among these minimum values from each rectangle.
- Count how many rectangles have this maximum square side.
- The solution requires a simple linear scan through the array with constant extra space.
Space and Time Complexity
Time Complexity: O(n), where n is the number of rectangles. Space Complexity: O(1) extra space.
Solution
The approach involves iterating over the list of rectangles to determine the maximum square side that can be obtained (by taking the minimum of each rectangle's dimensions). Once the maximum side (maxLen) is identified, iterate again (or count simultaneously) to count how many rectangles have that square side. This leverages simple arithmetic and comparisons in a single or double pass over the array, ensuring an efficient solution.