🚀

Thanksgiving Sale: Use Coupon Code THANKS25 to Get Extra 25% Off.

00DAYS
:
00HOURS
:
00MINUTES
:
00SECONDS

Leetcode Problem 2001. Number of Pairs of Interchangeable Rectangles

2001. Number of Pairs of Interchangeable Rectangles

Leetcode Solutions

Using GCD and HashMap

  1. Initialize a HashMap to store the frequency of each unique ratio as a pair of integers.
  2. 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.
  3. Initialize a variable to store the total number of interchangeable pairs.
  4. 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.
  5. Return the total number of interchangeable pairs.
UML Thumbnail

Using Double as HashMap Key

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...