LeetCode 2001: Number of Pairs of Interchangeable Rectangles Solution

Master LeetCode problem 2001 (Number of Pairs of Interchangeable Rectangles), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2001. Number of Pairs of Interchangeable Rectangles

Problem Explanation

Explanation

To solve this problem, we can iterate through the given rectangles and calculate the width-to-height ratio for each rectangle. We can store these ratios in a hashmap where the key is the ratio and the value is the count of rectangles with that specific ratio. For each ratio, we can calculate the number of pairs using the formula n * (n-1) / 2, where n is the count of rectangles with that ratio. Finally, we sum up all the pairs for each ratio to get the total number of interchangeable pairs.

Algorithm:

  1. Create a hashmap to store the count of rectangles with each ratio.
  2. Iterate through the rectangles and calculate the width-to-height ratio for each rectangle.
  3. Update the hashmap with the count of rectangles having that ratio.
  4. For each ratio, calculate the number of pairs using the formula n * (n-1) / 2, where n is the count of rectangles with that ratio.
  5. Sum up all the pairs for each ratio to get the total number of interchangeable pairs.

Time Complexity:

The time complexity of this solution is O(n), where n is the number of rectangles.

Space Complexity:

The space complexity of this solution is O(n), where n is the number of rectangles.

Solution Code

class Solution {
    public long interchangeableRectangles(int[][] rectangles) {
        HashMap<Double, Integer> map = new HashMap<>();
        long count = 0;
        
        for (int[] rect : rectangles) {
            double ratio = (double) rect[0] / rect[1];
            map.put(ratio, map.getOrDefault(ratio, 0) + 1);
        }
        
        for (int n : map.values()) {
            count += (long) n * (n - 1) / 2;
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2001 (Number of Pairs of Interchangeable Rectangles)?

This page provides optimized solutions for LeetCode problem 2001 (Number of Pairs of Interchangeable Rectangles) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2001 (Number of Pairs of Interchangeable Rectangles)?

The time complexity for LeetCode 2001 (Number of Pairs of Interchangeable Rectangles) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2001 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2001 in Java, C++, or Python.

Back to LeetCode Solutions