Skip to content

1725. Number Of Rectangles That Can Form The Largest Square 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  int countGoodRectangles(vector<vector<int>>& rectangles) {
    vector<int> minSides;

    for (const vector<int>& rectangle : rectangles) {
      const int x = rectangle[0];
      const int y = rectangle[1];
      minSides.push_back(min(x, y));
    }

    const int maxLen = ranges::max(minSides);
    return ranges::count(minSides, maxLen);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public int countGoodRectangles(int[][] rectangles) {
    int[] minSides = new int[rectangles.length];

    for (int i = 0; i < rectangles.length; ++i) {
      final int x = rectangles[i][0];
      final int y = rectangles[i][1];
      minSides[i] = Math.min(x, y);
    }

    final int maxLen = Arrays.stream(minSides).max().getAsInt();
    return (int) Arrays.stream(minSides).filter(minSide -> minSide == maxLen).count();
  }
}
1
2
3
4
class Solution:
  def countGoodRectangles(self, rectangles: list[list[int]]) -> int:
    minSides = [min(x, y) for x, y in rectangles]
    return minSides.count(max(minSides))