#2001

Number of Pairs of Interchangeable Rectangles

specialist · 840 · lc medium +31 · verified · 52.4% accepted · 580 likes · top 42%

Description

You have n rectangles in a 0-indexed 2D array rectangles where rectangles[i] = [widthi, heighti]. Two rectangles with indices i < j are interchangeable when their aspect ratios are equal: widthi / heighti == widthj / heightj (using real division).

Return the total number of interchangeable rectangle pairs.

Example 1:

Input: rectangles = [[4,8],[3,6],[10,20],[15,30]]
Output: 6
Explanation: The following are the interchangeable pairs of rectangles by index (0-indexed):
- Rectangle 0 with rectangle 1: 4/8 == 3/6.
- Rectangle 0 with rectangle 2: 4/8 == 10/20.
- Rectangle 0 with rectangle 3: 4/8 == 15/30.
- Rectangle 1 with rectangle 2: 3/6 == 10/20.
- Rectangle 1 with rectangle 3: 3/6 == 15/30.
- Rectangle 2 with rectangle 3: 10/20 == 15/30.

Example 2:

Input: rectangles = [[4,5],[7,8]]
Output: 0
Explanation: There are no interchangeable pairs of rectangles.

Code

1
2
3