Python 1725.py
class Solution:
def countGoodRectangles(self, rectangles: List[List[int]]) -> int:
squares = defaultdict(int)
for a, b in rectangles:
squares[min(a, b)] += 1
return squares[max(squares)]
Difficulty
and has an acceptance rate of about 80% (100k+ submissions).
Typical patterns to solve this problem:
- Array
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a class Solution with a single method countGoodRectangles. The method receives a collection of rectangle dimensions, each represented as a two-element list [a, b]. Its purpose is to determine how many rectangles share the largest possible side length that can form a square when the shorter side of each rectangle is considered. The algorithm builds a frequency map of the minimal side lengths and then extracts the count associated with the most frequent minimal side.
Key Steps
The core logic is expressed in three distinct statements.
The first statement creates a frequency container that automatically supplies a default integer value for any new key:
squares = defaultdict(int)
The second statement iterates over each rectangle, extracts the two side lengths, computes the smaller of the two, and increments the corresponding entry in the frequency container:
for a, b in rectangles:
squares[min(a, b)] += 1
The final statement identifies the key with the greatest numeric value among all keys in the frequency container and returns the count stored under that key:
return squares[max(squares)]
Edge Cases Handled
The algorithm naturally accommodates inputs where many rectangles share the same minimal side length. By aggregating counts in the defaultdict, any number of rectangles that reduce to the same square side are tallied correctly. When the input contains rectangles of varying dimensions, each distinct minimal side becomes a separate entry, ensuring that the frequency map reflects the distribution accurately. The use of the built-in max function on the dictionary keys guarantees that the largest minimal side present in the data set is selected, even when that side appears only once.
Performance Optimizations
The implementation leverages a single pass over the input list, resulting in a linear traversal without auxiliary data structures beyond the frequency map. The defaultdict eliminates the need for explicit existence checks before incrementing a count, reducing conditional overhead. Computing the minimal side with the built-in min function is a constant-time operation, and the final lookup using max(squares) accesses the dictionary directly, avoiding additional iteration over values. Together, these choices keep the runtime tightly bound to the size of the input while maintaining minimal memory consumption proportional to the number of distinct minimal side lengths.
ELI5 - Simplified Explanation
Imagine you have a big pile of paper rectangles and you want to see how many perfect squares you can cut out of them. A square needs to have all sides the same length, so for every rectangle, you look at the short side and the long side. You choose the shorter side because that is the biggest square you can possibly fit inside.
The code starts by getting a special box to keep track of these square sizes. It looks at every rectangle one by one. For each one, it finds the smaller number and puts a mark in the box for that size. If it sees a size it has already counted, it just adds another mark to that same spot.
Once all the rectangles have been checked, it looks at the box to find the largest size mark. Then it counts how many copies of that biggest square were found. This tells you which square size was the winner and exactly how many of them were hidden in your pile of rectangles.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||||
| www | AlgoMonster | C++ | Python | Java | TS | |||||
| WalkCCC | C++ | Python | Java | |||||||
| leetcode.ca | C++ | Python | Java | Go | TS | |||||
| Progiez | C++ | Python | Java | |||||||
| github | black-shadows | C++ | ||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JS | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| halfrost | Go | |||||||||
| haoel | C++ | |||||||||
| JoshCrozier | JS | |||||||||
| lkwq007 | Python | |||||||||
| suxrobGM | C# | |||||||||
| trung-hn | Python | |||||||||
| varunu28 | Java | |||||||||
| YuriSpiridonov | Python | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C++, Go, Java, Python, TypeScript | |||||||||
| Progiez | C++, Java, Python | |||||||||
| black-shadows | C++, C++ | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| everthis | JavaScript | |||||||||
| fartem | Ruby | |||||||||
| fsq | C++ | |||||||||
| halfrost | Go | |||||||||
| haoel | C++ | |||||||||
| JoshCrozier | JavaScript | |||||||||
| lkwq007 | Python | |||||||||
| suxrobGM | C# | |||||||||
| trung-hn | Python | |||||||||
| varunu28 | Java | |||||||||
| YuriSpiridonov | Python |
Videos
Solved On The Same Day
- problem 1636: Sort Array by Increasing Frequency
- problem 1652: Defuse the Bomb
- problem 1704: Determine if String Halves Are Alike
- problem 2305: Fair Distribution of Cookies
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality