- take the minimum element between the length and width for each rectangle, which will form a square
- record the max of the above elements as we iterate thorugh the array
- have a counter for every instance of the maxElement
π€What Did I Struggle With?
got this quickly in the interview
π‘ Explanation of Solution
same as intuition
β Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
π» Implementation of Solution
class Solution {public: int countGoodRectangles(vector<vector<int>>& rectangles) { int maxSquare = INT_MIN; int counter = 0; for(auto rectangle : rectangles) { int possibleSquare = min(rectangle[0], rectangle[1]); if(possibleSquare > maxSquare) { maxSquare = possibleSquare; counter = 1; } else if(possibleSquare == maxSquare) counter++; } return counter; }};