LeetCode 1725. The number of rectangles that can form the largest square

Give you an array of rectangles, where rectangles[i] = [li, wi] means that the length of the i-th rectangle is li and the width is wi.

If there is k satisfying k <= li and k <= wi at the same time, the i-th rectangle can be cut into a square with side length k. For example, the rectangle [4,6] can be cut into a square with a side length of up to 4.

Let maxLen be the side length of the largest square that can be divided from the rectangle array rectangles.

Returns the number of rectangles that can be cut out of a square with side length maxLen.

1 <= rectangles.length <= 1000
rectangles[i].length == 2
1 <= li, wi <= 109
li != wi

Traverse the array, and when the side length of the largest square that can be cut out of the current traversed rectangle is longer than the current maximum, update the maximum side length and initialize the number of squares that can be cut out; the largest square that can be cut out of the traversed rectangle When the side length is equal to the maximum side length of the current square, increase the number of cuts:

class Solution {
    
    
public:
    int countGoodRectangles(vector<vector<int>>& rectangles) {
    
    
        if (!rectangles.size()) {
    
    
            return 0;
        }

        int res = 1;
        int maxLen = 0;
        for (vector<int> &rectangle : rectangles) {
    
    
            int maxI = min(rectangle[0], rectangle[1]);

            if (maxI > maxLen) {
    
    
                res = 1;
                maxLen = maxI;
            } else if (maxI == maxLen) {
    
    
                ++res;
            }
        }

        return res;
    }
};

Guess you like

Origin blog.csdn.net/tus00000/article/details/112854885