Skip to content

Commit 8393d6e

Browse files
committed
feat: add solutions to lc problem: No.2001
No.2001.Number of Pairs of Interchangeable Rectangles
1 parent ce3e3b0 commit 8393d6e

File tree

6 files changed

+234
-2
lines changed

6 files changed

+234
-2
lines changed

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,106 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:数学 + 哈希表**
55+
56+
为了能够唯一表示矩形,我们需要将矩形的宽高比化简为最简分数。因此,我们可以求出每个矩形的宽高比的最大公约数,然后将宽高比化简为最简分数。接下来,我们使用哈希表统计每个最简分数的矩形数量,然后计算每个最简分数的矩形数量的组合数,即可得到答案。
57+
58+
时间复杂度 $O(n \log M)$,空间复杂度 $O(n)$。其中 $n$ 和 $M$ 分别是矩形的数量和矩形的最大边长。
59+
5460
<!-- tabs:start -->
5561

5662
### **Python3**
5763

5864
<!-- 这里可写当前语言的特殊实现逻辑 -->
5965

6066
```python
61-
67+
class Solution:
68+
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
69+
ans = 0
70+
cnt = Counter()
71+
for w, h in rectangles:
72+
g = gcd(w, h)
73+
w, h = w // g, h // g
74+
ans += cnt[(w, h)]
75+
cnt[(w, h)] += 1
76+
return ans
6277
```
6378

6479
### **Java**
6580

6681
<!-- 这里可写当前语言的特殊实现逻辑 -->
6782

6883
```java
84+
class Solution {
85+
public long interchangeableRectangles(int[][] rectangles) {
86+
long ans = 0;
87+
int n = rectangles.length + 1;
88+
Map<Long, Integer> cnt = new HashMap<>();
89+
for (var e : rectangles) {
90+
int w = e[0], h = e[1];
91+
int g = gcd(w, h);
92+
w /= g;
93+
h /= g;
94+
long x = (long) w * n + h;
95+
ans += cnt.getOrDefault(x, 0);
96+
cnt.merge(x, 1, Integer::sum);
97+
}
98+
return ans;
99+
}
100+
101+
private int gcd(int a, int b) {
102+
return b == 0 ? a : gcd(b, a % b);
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
long long interchangeableRectangles(vector<vector<int>>& rectangles) {
113+
long long ans = 0;
114+
int n = rectangles.size();
115+
unordered_map<long long, int> cnt;
116+
for (auto& e : rectangles) {
117+
int w = e[0], h = e[1];
118+
int g = gcd(w, h);
119+
w /= g;
120+
h /= g;
121+
long long x = 1ll * w * (n + 1) + h;
122+
ans += cnt[x];
123+
cnt[x]++;
124+
}
125+
return ans;
126+
}
127+
};
128+
```
69129
130+
### **Go**
131+
132+
```go
133+
func interchangeableRectangles(rectangles [][]int) int64 {
134+
ans := 0
135+
n := len(rectangles)
136+
cnt := map[int]int{}
137+
for _, e := range rectangles {
138+
w, h := e[0], e[1]
139+
g := gcd(w, h)
140+
w, h = w/g, h/g
141+
x := w*(n+1) + h
142+
ans += cnt[x]
143+
cnt[x]++
144+
}
145+
return int64(ans)
146+
}
147+
148+
func gcd(a, b int) int {
149+
if b == 0 {
150+
return a
151+
}
152+
return gcd(b, a%b)
153+
}
70154
```
71155

72156
### **...**

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,91 @@
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
55+
ans = 0
56+
cnt = Counter()
57+
for w, h in rectangles:
58+
g = gcd(w, h)
59+
w, h = w // g, h // g
60+
ans += cnt[(w, h)]
61+
cnt[(w, h)] += 1
62+
return ans
5463
```
5564

5665
### **Java**
5766

5867
```java
68+
class Solution {
69+
public long interchangeableRectangles(int[][] rectangles) {
70+
long ans = 0;
71+
int n = rectangles.length + 1;
72+
Map<Long, Integer> cnt = new HashMap<>();
73+
for (var e : rectangles) {
74+
int w = e[0], h = e[1];
75+
int g = gcd(w, h);
76+
w /= g;
77+
h /= g;
78+
long x = (long) w * n + h;
79+
ans += cnt.getOrDefault(x, 0);
80+
cnt.merge(x, 1, Integer::sum);
81+
}
82+
return ans;
83+
}
84+
85+
private int gcd(int a, int b) {
86+
return b == 0 ? a : gcd(b, a % b);
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
long long interchangeableRectangles(vector<vector<int>>& rectangles) {
97+
long long ans = 0;
98+
int n = rectangles.size();
99+
unordered_map<long long, int> cnt;
100+
for (auto& e : rectangles) {
101+
int w = e[0], h = e[1];
102+
int g = gcd(w, h);
103+
w /= g;
104+
h /= g;
105+
long long x = 1ll * w * (n + 1) + h;
106+
ans += cnt[x];
107+
cnt[x]++;
108+
}
109+
return ans;
110+
}
111+
};
112+
```
59113
114+
### **Go**
115+
116+
```go
117+
func interchangeableRectangles(rectangles [][]int) int64 {
118+
ans := 0
119+
n := len(rectangles)
120+
cnt := map[int]int{}
121+
for _, e := range rectangles {
122+
w, h := e[0], e[1]
123+
g := gcd(w, h)
124+
w, h = w/g, h/g
125+
x := w*(n+1) + h
126+
ans += cnt[x]
127+
cnt[x]++
128+
}
129+
return int64(ans)
130+
}
131+
132+
func gcd(a, b int) int {
133+
if b == 0 {
134+
return a
135+
}
136+
return gcd(b, a%b)
137+
}
60138
```
61139

62140
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
long long interchangeableRectangles(vector<vector<int>>& rectangles) {
4+
long long ans = 0;
5+
int n = rectangles.size();
6+
unordered_map<long long, int> cnt;
7+
for (auto& e : rectangles) {
8+
int w = e[0], h = e[1];
9+
int g = gcd(w, h);
10+
w /= g;
11+
h /= g;
12+
long long x = 1ll * w * (n + 1) + h;
13+
ans += cnt[x];
14+
cnt[x]++;
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func interchangeableRectangles(rectangles [][]int) int64 {
2+
ans := 0
3+
n := len(rectangles)
4+
cnt := map[int]int{}
5+
for _, e := range rectangles {
6+
w, h := e[0], e[1]
7+
g := gcd(w, h)
8+
w, h = w/g, h/g
9+
x := w*(n+1) + h
10+
ans += cnt[x]
11+
cnt[x]++
12+
}
13+
return int64(ans)
14+
}
15+
16+
func gcd(a, b int) int {
17+
if b == 0 {
18+
return a
19+
}
20+
return gcd(b, a%b)
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public long interchangeableRectangles(int[][] rectangles) {
3+
long ans = 0;
4+
int n = rectangles.length + 1;
5+
Map<Long, Integer> cnt = new HashMap<>();
6+
for (var e : rectangles) {
7+
int w = e[0], h = e[1];
8+
int g = gcd(w, h);
9+
w /= g;
10+
h /= g;
11+
long x = (long) w * n + h;
12+
ans += cnt.getOrDefault(x, 0);
13+
cnt.merge(x, 1, Integer::sum);
14+
}
15+
return ans;
16+
}
17+
18+
private int gcd(int a, int b) {
19+
return b == 0 ? a : gcd(b, a % b);
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
3+
ans = 0
4+
cnt = Counter()
5+
for w, h in rectangles:
6+
g = gcd(w, h)
7+
w, h = w // g, h // g
8+
ans += cnt[(w, h)]
9+
cnt[(w, h)] += 1
10+
return ans

0 commit comments

Comments
 (0)