Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: rectangles = [[5,8],[3,9],[5,12],[16,5]] Output: 3 Explanation: The largest squares you can get from each rectangle are of lengths [5,3,5,5]. The largest possible square is of length 5, and you can get it out of 3 rectangles.
Example 2:
Input: rectangles = [[2,3],[3,7],[4,3],[3,7]] Output: 3
Constraints:
1 <= rectangles.length <= 1000rectangles[i].length == 21 <= li, wi <= 109li != wiProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[5,8],[3,9],[5,12],[16,5]]
[[2,3],[3,7],[4,3],[3,7]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1725: Number Of Rectangles That Can Form The Largest Square
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int ans = 0, mx = 0;
for (var e : rectangles) {
int x = Math.min(e[0], e[1]);
if (mx < x) {
mx = x;
ans = 1;
} else if (mx == x) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1725: Number Of Rectangles That Can Form The Largest Square
func countGoodRectangles(rectangles [][]int) (ans int) {
mx := 0
for _, e := range rectangles {
x := min(e[0], e[1])
if mx < x {
mx = x
ans = 1
} else if mx == x {
ans++
}
}
return
}
# Accepted solution for LeetCode #1725: Number Of Rectangles That Can Form The Largest Square
class Solution:
def countGoodRectangles(self, rectangles: List[List[int]]) -> int:
ans = mx = 0
for l, w in rectangles:
x = min(l, w)
if mx < x:
ans = 1
mx = x
elif mx == x:
ans += 1
return ans
// Accepted solution for LeetCode #1725: Number Of Rectangles That Can Form The Largest Square
struct Solution;
use std::collections::HashMap;
impl Solution {
fn count_good_rectangles(rectangles: Vec<Vec<i32>>) -> i32 {
let mut count: HashMap<i32, usize> = HashMap::new();
let mut max = 0;
for rect in rectangles {
let min = rect[0].min(rect[1]);
*count.entry(min).or_default() += 1;
max = max.max(min);
}
count[&max] as i32
}
}
#[test]
fn test() {
let rectangles = vec_vec_i32![[5, 8], [3, 9], [5, 12], [16, 5]];
let res = 3;
assert_eq!(Solution::count_good_rectangles(rectangles), res);
let rectangles = vec_vec_i32![[2, 3], [3, 7], [4, 3], [3, 7]];
let res = 3;
assert_eq!(Solution::count_good_rectangles(rectangles), res);
}
// Accepted solution for LeetCode #1725: Number Of Rectangles That Can Form The Largest Square
function countGoodRectangles(rectangles: number[][]): number {
let [ans, mx] = [0, 0];
for (const [l, w] of rectangles) {
const x = Math.min(l, w);
if (mx < x) {
mx = x;
ans = 1;
} else if (mx === x) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.