EasyNo. 1725

Number Of Rectangles That Can Form The Largest Square — LeetCode 1725 Python Solution

Topics
Array
Reading time
2 min

The problem

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.

Example

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].

Python solution

PythonSolution
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

Complexity

Time
O(n)where n is the length of the array rectangles
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

Frequently asked questions

How hard is LeetCode 1725. Number Of Rectangles That Can Form The Largest Square?

LeetCode 1725. Number Of Rectangles That Can Form The Largest Square is rated Easy on LeetCode.

What is the time complexity of LeetCode 1725. Number Of Rectangles That Can Form The Largest Square?

The Python solution on this page runs in O(n), where n is the length of the array rectangles.

What is the space complexity of LeetCode 1725. Number Of Rectangles That Can Form The Largest Square?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 1725. Number Of Rectangles That Can Form The Largest Square cover?

LeetCode 1725. Number Of Rectangles That Can Form The Largest Square is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.