返回介绍

solution / 2000-2099 / 2001.Number of Pairs of Interchangeable Rectangles / README_EN

发布于 2024-06-17 01:03:12 字数 5526 浏览 0 评论 0 收藏 0

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:
  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
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;
  }
};
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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文