0
点赞
收藏
分享

微信扫一扫

1725.可以形成最大正方形的矩形数目

古得曼_63b6 2022-02-04 阅读 55

题目

1725.可以形成最大正方形的矩形数目

题目大意

给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi

如果存在 k 同时满足 k <= l_ik <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 4 的正方形。

maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。

请你统计有多少个矩形能够切出边长为 maxLen 的正方形,并返回矩形 数目

样例

image-20220204213631034

数据规模

image-20220204213639501

思路

i i i个矩形切成边长为的正方形的边长实际上就是第 i i i个长方形的 m i n ( l i , w i ) min(l_i,w_i) min(li,wi),即min(rectangles[i][0],rectangles[i][1]),那么考虑取 m a x ( m i n ( r e c t a n g l e s [ i ] [ 0 ] , r e c t a n g l e s [ i ] [ 1 ] ) ( i ∈ [ 0 , n − 1 ] ) ) max(min(rectangles[i][0],rectangles[i][1])(i∈[0,n-1])) max(min(rectangles[i][0],rectangles[i][1])(i[0,n1]))为最大正方形的边长。设定 m a x x maxx maxx为最大正方形的边长,每次需要更新 m a x x maxx maxx的时候,需要将 c n t cnt cnt置为 1 1 1( c n t cnt cnt表示该边长的正方形的个数),如果当前长方形可切出的正方形边长为 m a x x maxx maxx,那么 c n t + + cnt++ cnt++。最后返回答案 c n t cnt cnt

代码

class Solution {
public:
    int countGoodRectangles(vector<vector<int>>& rectangles) {
        int maxx=0,cnt=0;
        for(int i=0;i<rectangles.size();i++){
            int l=min(rectangles[i][0],rectangles[i][1]);
            if(maxx<l){
                maxx=l;
                cnt=1;
            }
            else if(maxx==l){
                cnt++;
            }
        }
        return cnt;
    }
};
举报

相关推荐

0 条评论