Skip to content

Commit b10a8fc

Browse files
authored
Create number-of-pairs-of-interchangeable-rectangles.py
1 parent 440a066 commit b10a8fc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
import collections
5+
import fractions
6+
7+
8+
class Solution(object):
9+
def interchangeableRectangles(self, rectangles):
10+
"""
11+
:type rectangles: List[List[int]]
12+
:rtype: int
13+
"""
14+
count = collections.defaultdict(int)
15+
for w, h in rectangles:
16+
g = fractions.gcd(w, h) # Time: O((logx)^2) ~= O(1)
17+
count[(w//g, h//g)] += 1
18+
return sum(c*(c-1)//2 for c in count.itervalues())

0 commit comments

Comments
 (0)