Number of pairs of interchangeable rectangles
December 9, 2022
array-and-hashmapProblem URL: Number of pairs of interchangeable rectangles
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)