2001. Number of Pairs of Interchangeable Rectangles Medium
1/**
2 * [2001] Number of Pairs of Interchangeable Rectangles
3 *
4 * 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 i^th rectangle.
5 * 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).
6 * Return the number of pairs of interchangeable rectangles in rectangles.
7 *
8 * Example 1:
9 *
10 * Input: rectangles = [[4,8],[3,6],[10,20],[15,30]]
11 * Output: 6
12 * Explanation: The following are the interchangeable pairs of rectangles by index (0-indexed):
13 * - Rectangle 0 with rectangle 1: 4/8 == 3/6.
14 * - Rectangle 0 with rectangle 2: 4/8 == 10/20.
15 * - Rectangle 0 with rectangle 3: 4/8 == 15/30.
16 * - Rectangle 1 with rectangle 2: 3/6 == 10/20.
17 * - Rectangle 1 with rectangle 3: 3/6 == 15/30.
18 * - Rectangle 2 with rectangle 3: 10/20 == 15/30.
19 *
20 * Example 2:
21 *
22 * Input: rectangles = [[4,5],[7,8]]
23 * Output: 0
24 * Explanation: There are no interchangeable pairs of rectangles.
25 *
26 *
27 * Constraints:
28 *
29 * n == rectangles.length
30 * 1 <= n <= 10^5
31 * rectangles[i].length == 2
32 * 1 <= widthi, heighti <= 10^5
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/
38// discuss: https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn interchangeable_rectangles(rectangles: Vec<Vec<i32>>) -> i64 {
44
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_2001() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.