Leetcode 1725: Number Of Rectangles That Can Form The Largest Square

grid47
grid47
Exploring patterns and algorithms
May 18, 2024 5 min read

You are given an array rectangles where each element rectangles[i] = [li, wi] represents a rectangle with length li and width wi. You can cut a square from the rectangle as long as the side of the square is less than or equal to both the length and width of the rectangle. Your task is to find the largest possible square that can be cut from any of the given rectangles, and then return how many rectangles can produce that largest square.
Problem
Approach
Steps
Complexity
Input: You are given an array `rectangles`, where each element represents a rectangle's length and width. Each rectangle is represented as a list of two integers.
Example: Input: rectangles = [[6, 9], [3, 8], [5, 7], [12, 6]]
Constraints:
• 1 <= rectangles.length <= 1000
• rectangles[i].length == 2
• 1 <= li, wi <= 10^9
• li != wi
Output: Return the number of rectangles that can form a square with the largest possible side length.
Example: Output: 2
Constraints:
• The output is an integer representing the count of rectangles capable of forming the largest square.
Goal: The goal is to find the largest square that can be formed from any of the given rectangles and then determine how many rectangles can form that square.
Steps:
• 1. For each rectangle, determine the largest square that can be cut from it. This is done by taking the minimum of its length and width.
• 2. Track the largest square found so far, and count how many rectangles can produce this square.
• 3. Return the count of rectangles that can form the largest square.
Goal: The solution should efficiently handle up to 1000 rectangles and side lengths and widths as large as 10^9.
Steps:
• 1 <= rectangles.length <= 1000
• 1 <= li, wi <= 10^9
• li != wi
Assumptions:
• Each rectangle is non-degenerate, meaning both the length and width are non-zero.
Input: Input: rectangles = [[6, 9], [3, 8], [5, 7], [12, 6]]
Explanation: The largest squares from the rectangles have side lengths [6, 3, 5, 6]. The largest square has side length 6, and two rectangles can form a square of side 6, so the output is 2.

Input: Input: rectangles = [[7, 8], [5, 5], [9, 10], [6, 6]]
Explanation: The largest squares from the rectangles have side lengths [7, 5, 9, 6]. The largest square has side length 9, and only one rectangle can form a square of side 9, so the output is 1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus