array and hashmap December 9, 2022

Number of pairs of interchangeable rectangles

Time O(n) Space O(n) Open original problem

We will use a hash map to store the number of rectangles with each width and height. Then we will iterate over the hash map and count the number of pairs of rectangles that can be interchanged.

class Solution:
    def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
        lookup = collections.defaultdict(int)
        for width, height in rectangles:
            lookup[width/height] += 1

        res = 0
        for num in lookup.values():
            res += num * (num-1) // 2

        return res

Time complexity: O(n)
Space complexity: O(n)