可以形成最大正方形的矩形数目
题目描述
可以形成最大正方形的矩形数目
思路
模拟
根据题意,遍历rectangles数组,找到最大的正方形边长统计即可。
Java实现
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int ans = 0, maxLen = 0;
for (int i = 0; i < rectangles.length; ++i) {
int l = rectangles[i][0], w = rectangles[i][1];
int maxLeni = Math.min(l, w);
if (maxLen == maxLeni) {
ans++;
} else if (maxLeni > maxLen) {
maxLen = Math.max(maxLen, maxLeni);
ans = 1;
}
}
return ans;
}
}
Python实现
class Solution:
def countGoodRectangles(self, rectangles: List[List[int]]) -> int:
res, maxLen = 0, 0
for l, w in rectangles:
k = min(l, w)
if k == maxLen:
res += 1
elif k > maxLen:
res = 1
maxLen = k
return res