#1725
Easy Algorithms Number of rectangles that can form the largest square
Array
79.4% acceptance
Feb 25, 2026
624
75
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.
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_good_rectangles(rectangles: Vec<Vec<i32>>) -> i32 {
let sides: Vec<i32> = rectangles.iter().map(|r| r[0].min(r[1])).collect();
let max_len = *sides.iter().max().unwrap();
sides.iter().filter(|&&s| s == max_len).count() as i32
}
}