Welcome to Subscribe On Youtube

2001. Number of Pairs of Interchangeable Rectangles

Description

You are given n rectangles represented by a 0-indexed 2D integer array rectangles, where rectangles[i] = [widthi, heighti] denotes the width and height of the ith rectangle.

Two rectangles i and j (i < j) are considered interchangeable if they have the same width-to-height ratio. More formally, two rectangles are interchangeable if widthi/heighti == widthj/heightj (using decimal division, not integer division).

Return the number of pairs of interchangeable rectangles in rectangles.

 

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.

 

Constraints:

  • n == rectangles.length
  • 1 <= n <= 105
  • rectangles[i].length == 2
  • 1 <= widthi, heighti <= 105

Solutions

Solution 1: Mathematics + Hash Table

In order to uniquely represent a rectangle, we need to simplify the width-to-height ratio of the rectangle to a simplest fraction. Therefore, we can find the greatest common divisor of the width-to-height ratio of each rectangle, and then simplify the width-to-height ratio to the simplest fraction. Next, we use a hash table to count the number of rectangles for each simplest fraction, and then calculate the combination of the number of rectangles for each simplest fraction to get the answer.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Here, $n$ and $M$ are the number of rectangles and the maximum side length of the rectangles, respectively.

  • class Solution {
        public long interchangeableRectangles(int[][] rectangles) {
            long ans = 0;
            int n = rectangles.length + 1;
            Map<Long, Integer> cnt = new HashMap<>();
            for (var e : rectangles) {
                int w = e[0], h = e[1];
                int g = gcd(w, h);
                w /= g;
                h /= g;
                long x = (long) w * n + h;
                ans += cnt.getOrDefault(x, 0);
                cnt.merge(x, 1, Integer::sum);
            }
            return ans;
        }
    
        private int gcd(int a, int b) {
            return b == 0 ? a : gcd(b, a % b);
        }
    }
    
  • class Solution {
    public:
        long long interchangeableRectangles(vector<vector<int>>& rectangles) {
            long long ans = 0;
            int n = rectangles.size();
            unordered_map<long long, int> cnt;
            for (auto& e : rectangles) {
                int w = e[0], h = e[1];
                int g = gcd(w, h);
                w /= g;
                h /= g;
                long long x = 1ll * w * (n + 1) + h;
                ans += cnt[x];
                cnt[x]++;
            }
            return ans;
        }
    };
    
  • class Solution:
        def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
            ans = 0
            cnt = Counter()
            for w, h in rectangles:
                g = gcd(w, h)
                w, h = w // g, h // g
                ans += cnt[(w, h)]
                cnt[(w, h)] += 1
            return ans
    
    
  • func interchangeableRectangles(rectangles [][]int) int64 {
    	ans := 0
    	n := len(rectangles)
    	cnt := map[int]int{}
    	for _, e := range rectangles {
    		w, h := e[0], e[1]
    		g := gcd(w, h)
    		w, h = w/g, h/g
    		x := w*(n+1) + h
    		ans += cnt[x]
    		cnt[x]++
    	}
    	return int64(ans)
    }
    
    func gcd(a, b int) int {
    	if b == 0 {
    		return a
    	}
    	return gcd(b, a%b)
    }
    
  • /**
     * @param {number[][]} rectangles
     * @return {number}
     */
    var interchangeableRectangles = function (rectangles) {
        const cnt = new Map();
        let ans = 0;
        for (let [w, h] of rectangles) {
            const g = gcd(w, h);
            w = Math.floor(w / g);
            h = Math.floor(h / g);
            const x = w * (rectangles.length + 1) + h;
            ans += cnt.get(x) | 0;
            cnt.set(x, (cnt.get(x) | 0) + 1);
        }
        return ans;
    };
    
    function gcd(a, b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
    
    

All Problems

All Solutions