Leetcode Problem 2001. Number of Pairs of Interchangeable Rectangles
2001. Number of Pairs of Interchangeable Rectangles
Leetcode Solutions
Using GCD and HashMap
Initialize a HashMap to store the frequency of each unique ratio as a pair of integers.
Iterate over each rectangle in the input array.
a. Calculate the GCD of the width and height.
b. Divide the width and height by their GCD to get the simplest form.
c. Use the pair (width/GCD, height/GCD) as the key in the HashMap and increment its frequency.
Initialize a variable to store the total number of interchangeable pairs.
Iterate over the entries in the HashMap.
a. For each entry, calculate the number of pairs using the formula count * (count - 1) / 2.
b. Add the result to the total number of interchangeable pairs.