class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
res = 0
for i in range(1, len(rectangles)):
for j in range(i):
if rectangles[i][0] / rectangles[i][1] == rectangles[j][0] / rectangles[j][1]:
res += 1
return resclass Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
count = {}
for w, h in rectangles:
count[w / h] = 1 + count.get(w / h, 0)
res = 0
for c in count.values():
if c > 1:
res += (c * (c - 1)) // 2
return resclass Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
count = {}
res = 0
for w, h in rectangles:
res += count.get(w / h, 0)
count[w / h] = 1 + count.get(w / h, 0)
return resclass Solution:
def hash(self, a: int, b: int) -> int:
mask = a
mask |= (b << 31)
return mask
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
res = 0
count = {}
for rect in rectangles:
gcd = math.gcd(rect[0], rect[1])
key = self.hash(rect[0] // gcd, rect[1] // gcd)
res += count.get(key, 0)
count[key] = count.get(key, 0) + 1
return res