LeetCode 1725: Number Of Rectangles That Can Form The Largest Square Solution
Master LeetCode problem 1725 (Number Of Rectangles That Can Form The Largest Square), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1725. Number Of Rectangles That Can Form The Largest Square
Problem Explanation
Explanation
To solve this problem, we need to find the maximum side length possible for a square that can be formed from the given rectangles and then count how many rectangles can form a square of that side length. We can achieve this by iterating through all the rectangles, finding the minimum side length of each rectangle, and then keeping track of the maximum of these minimum side lengths. Finally, we count how many rectangles have a side length equal to the maximum side length found.
-
Algorithm:
- Initialize
maxLento store the maximum side length found. - Iterate through all rectangles and find the minimum side length of each rectangle.
- Update
maxLenwith the maximum of the minimum side lengths found. - Iterate through the rectangles again and count how many rectangles have a side length equal to
maxLen. - Return the count as the result.
- Initialize
-
Time Complexity: O(n) where n is the number of rectangles.
-
Space Complexity: O(1) as we are using constant extra space.
Solution Code
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int maxLen = 0;
int count = 0;
for (int[] rect : rectangles) {
int minSide = Math.min(rect[0], rect[1]);
maxLen = Math.max(maxLen, minSide);
}
for (int[] rect : rectangles) {
if (Math.min(rect[0], rect[1]) == maxLen) {
count++;
}
}
return count;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1725 (Number Of Rectangles That Can Form The Largest Square)?
This page provides optimized solutions for LeetCode problem 1725 (Number Of Rectangles That Can Form The Largest Square) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1725 (Number Of Rectangles That Can Form The Largest Square)?
The time complexity for LeetCode 1725 (Number Of Rectangles That Can Form The Largest Square) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1725 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1725 in Java, C++, or Python.