1725. 可以形成最大正方形的矩形数目
1725. 可以形成最大正方形的矩形数目
题目
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. For example, if you have a rectangle [4,6], you can cut it to get a square with a side length of at most 4.
Let maxLen be the side length of the largest square you can obtain from any of the given rectangles.
Return _the number of rectangles that can make a square with a side length of _maxLen.
Example 1:
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].
The largest possible square is of length 5, and you can get it out of 3 rectangles.
Example 2:
Input: rectangles = [[2,3],[3,7],[4,3],[3,7]]
Output: 3
Constraints:
1 <= rectangles.length <= 1000rectangles[i].length == 21 <= li, wi <= 10^9li != wi
题目大意
给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi 。
如果存在 k 同时满足 k <= li 和 k <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 4 的正方形。
设 maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。
请你统计有多少个矩形能够切出边长为 maxLen 的正方形,并返回矩形 数目 。
示例 1:
输入: rectangles = [[5,8],[3,9],[5,12],[16,5]]
输出: 3
解释: 能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。
最大正方形的边长为 5 ,可以由 3 个矩形切分得到。
示例 2:
输入: rectangles = [[2,3],[3,7],[4,3],[3,7]]
输出: 3
提示:
1 <= rectangles.length <= 1000rectangles[i].length == 21 <= li, wi <= 10^9li != wi
解题思路
初始化变量:
- 用
largest记录当前能够切出的最大正方形的边长,初始值为0。 - 用
count记录可以切出最大正方形的矩形数量,初始值为0。
- 用
遍历矩形:
- 对于每个矩形
(l, w),计算其可以切出的最大正方形边长len = Math.min(l, w)。 - 如果当前边长
len大于largest:- 更新
largest为当前边长。 - 重置
count为1。
- 更新
- 如果当前边长
len等于largest:- 增加
count。
- 增加
- 对于每个矩形
返回结果:
- 遍历完成后,返回
count。
- 遍历完成后,返回
复杂度分析
- 时间复杂度:
O(n),其中n是矩形的数量,需要遍历一次rectangles。 - 空间复杂度:
O(1),只使用了常数额外空间。
代码
/**
* @param {number[][]} rectangles
* @return {number}
*/
var countGoodRectangles = function (rectangles) {
let largest = 0;
let count = 0;
for (let [l, w] of rectangles) {
const len = Math.min(l, w); // 当前矩形能切出的最大正方形边长
if (largest < len) {
// 找到更大的正方形
count = 1; // 重置计数
largest = len; // 更新最大正方形边长
} else if (largest == len) {
count++; // 计数加一
}
}
return count;
};
