Number Of Rectangles That Can Form The Largest Square - Problem
You are given an array rectangles where rectangles[i] = [li, wi] represents the ith rectangle of length li and width wi.
You can cut the ith rectangle to form a square with a side length of k if both k <= li and k <= wi. For example, if you have a rectangle [4,6], you can cut it to get a square with a side length of at most 4.
Let maxLen be the side length of the largest square you can obtain from any of the given rectangles.
Return the number of rectangles that can make a square with a side length of maxLen.
Input & Output
Example 1 — Basic Case
$
Input:
rectangles = [[5,8],[3,9],[5,12],[16,5]]
›
Output:
3
💡 Note:
The largest square you can cut from each rectangle: [5,8]→5, [3,9]→3, [5,12]→5, [16,5]→5. The largest square size is 5, and 3 rectangles can form this size.
Example 2 — All Same Size
$
Input:
rectangles = [[2,3],[3,7],[4,3]]
›
Output:
2
💡 Note:
Maximum square sizes: [2,3]→2, [3,7]→3, [4,3]→3. The largest square size is 3, and 2 rectangles ([3,7] and [4,3]) can form this size.
Example 3 — Single Rectangle
$
Input:
rectangles = [[10,20]]
›
Output:
1
💡 Note:
Only one rectangle [10,20] can form a square of size min(10,20)=10. So 1 rectangle can form the maximum square.
Constraints
- 1 ≤ rectangles.length ≤ 1000
- rectangles[i].length == 2
- 1 ≤ li, wi ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code