Number of Rectangles That Can Form Largest Square

Easy

Description

Given an array of rectangles where rectangles[i] = [li, wi], return the number of rectangles that can form the largest possible square. A rectangle can form a square of side min(li, wi).

Examples

Input:rectangles = [[5,8],[3,9],[5,12],[16,5]]
Output:3
Explanation:

Max square side 5, 3 rectangles.

Input:rectangles = [[4,6],[2,8],[3,4],[4,7],[4,9]]
Output:3
Explanation:

The largest possible square side length is 4. A square with side 4 can be formed from rectangles [4,6], [4,7], and [4,9] since each has at least one dimension >= 4. Rectangle [2,8] has max dimension 8 >= 4 but min dimension 2 < 4, so it can form the square. Rectangle [3,4] also works. Total: 4 rectangles can form squares of side 4.

Input:rectangles = [[1,1],[2,2],[3,3]]
Output:1
Explanation:

The largest possible square side length is 3 (from rectangle [3,3]). Only the rectangle [3,3] can form a square with side length 3, since the other rectangles [1,1] and [2,2] are too small. Therefore, only 1 rectangle can form the largest possible square.

Constraints

  • 1 ≤ rectangles.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!