File: number-of-rectangles-that-can-form-the-largest-square/solution.py

Date: 2026-06-06

Time: 18:20

number-of-rectangles-that-can-form-the-largest-square/solution.py

Purpose

This file solves LeetCode 1725 — given a list of rectangles, determine how many can produce a square with the maximum possible side length. Each rectangle [l, w] can form a square with side min(l, w). The file owns the complete solution logic and exposes it via the standard Solution class.

Key Components

Solution.numberOfSets(rectangles: List[List[int]]) -> int — the single method. Note the method name numberOfSets doesn't match LeetCode's canonical countGoodRectangles, but the contract is identical: accept a list of [length, width] pairs, return the count of rectangles whose inscribed square side equals the global maximum.

Patterns

Single-pass max-tracking: Rather than computing all square sides, finding the max, then counting matches (two passes), this does both in one loop. When a new maximum is found, the count resets to 1; when a tie is found, the count increments. This is a common idiom across this repo's solutions — avoid materializing intermediate lists when a running accumulator suffices.

LeetCode Solution class convention: Every problem directory exposes a Solution class with the solving method, matching the structure LeetCode expects. The test files import from this module.

Dependencies

Imports: Only typing.List — no external libraries. The solution is self-contained.

Imported by: number-of-rectangles-that-can-form-the-largest-square/test_solution.py is the direct consumer. The massive "Imported By" list in the prompt is misleading — those are unrelated test files that each import their own local solution.py, not this one.

Flow

1. Initialize max_len = 0 and count = 0.

2. For each rectangle [l, w], compute side = min(l, w) — the largest square that fits.

3. If side > maxlen: new global max found, reset count = 1 and update maxlen.

4. Elif side == max_len: another rectangle ties the max, increment count.

5. Return count.

This is O(n) time, O(1) space.

Invariants

Error Handling

None. The method trusts its input matches the LeetCode contract (non-empty list of two-element integer lists). No validation, no exceptions. This is consistent with the repo's convention — solutions assume valid input per problem constraints.

Topics to Explore

Beliefs