Skip to content

Commit b9e714e

Browse files
committed
feat: add solutions to lc problem: No.1577
No.1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers
1 parent 383b8b1 commit b9e714e

File tree

5 files changed

+285
-2
lines changed

5 files changed

+285
-2
lines changed

solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README.md

+105-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,126 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:哈希表**
63+
64+
我们用哈希表 `cnt1` 统计 `nums1` 中每个数出现的次数,用哈希表 `cnt2` 统计 `nums2` 中每个数出现的次数。
65+
66+
然后我们双重循环遍历两个哈希表,记当前 `cnt1` 遍历到的键值对为 $(a, x)$,当前 `cnt2` 遍历到的键值对为 $(b, y)$。接下来分情况讨论:
67+
68+
- 如果 $a^2$ 能被 $b$ 整除,设 $c=\frac{a^2}{b}$,若 $b=c$,那么答案加上 $x \times y \times (y - 1)$,否则答案加上 $x \times y \times cnt2[c]$。
69+
- 如果 $b^2$ 能被 $a$ 整除,设 $c=\frac{b^2}{a}$,若 $a=c$,那么答案加上 $x \times (x - 1) \times y$,否则答案加上 $x \times cnt1[c] \times y$。
70+
71+
最后将答案除以 $2$ 返回即可。
72+
73+
时间复杂度 $O(n \times m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别为数组 `nums1``nums2` 的长度。
74+
6275
<!-- tabs:start -->
6376

6477
### **Python3**
6578

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

6881
```python
69-
82+
class Solution:
83+
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
84+
cnt1 = Counter(nums1)
85+
cnt2 = Counter(nums2)
86+
ans = 0
87+
for a, x in cnt1.items():
88+
for b, y in cnt2.items():
89+
if a * a % b == 0:
90+
c = a * a // b
91+
if b == c:
92+
ans += x * y * (y - 1)
93+
else:
94+
ans += x * y * cnt2[c]
95+
if b * b % a == 0:
96+
c = b * b // a
97+
if a == c:
98+
ans += x * (x - 1) * y
99+
else:
100+
ans += x * y * cnt1[c]
101+
return ans >> 1
70102
```
71103

72104
### **Java**
73105

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

76108
```java
109+
class Solution {
110+
public int numTriplets(int[] nums1, int[] nums2) {
111+
Map<Integer, Integer> cnt1 = new HashMap<>();
112+
Map<Integer, Integer> cnt2 = new HashMap<>();
113+
for (int v : nums1) {
114+
cnt1.put(v, cnt1.getOrDefault(v, 0) + 1);
115+
}
116+
for (int v : nums2) {
117+
cnt2.put(v, cnt2.getOrDefault(v, 0) + 1);
118+
}
119+
long ans = 0;
120+
for (var e1 : cnt1.entrySet()) {
121+
long a = e1.getKey(), x = e1.getValue();
122+
for (var e2 : cnt2.entrySet()) {
123+
long b = e2.getKey(), y = e2.getValue();
124+
if ((a * a) % b == 0) {
125+
long c = a * a / b;
126+
if (b == c) {
127+
ans += x * y * (y - 1);
128+
} else {
129+
ans += x * y * cnt2.getOrDefault((int) c, 0);
130+
}
131+
}
132+
if ((b * b) % a == 0) {
133+
long c = b * b / a;
134+
if (a == c) {
135+
ans += x * (x - 1) * y;
136+
} else {
137+
ans += x * y * cnt1.getOrDefault((int) c, 0);
138+
}
139+
}
140+
}
141+
}
142+
return (int) (ans >> 1);
143+
}
144+
}
145+
```
77146

147+
### **Go**
148+
149+
```go
150+
func numTriplets(nums1 []int, nums2 []int) (ans int) {
151+
cnt1 := map[int]int{}
152+
cnt2 := map[int]int{}
153+
for _, v := range nums1 {
154+
cnt1[v]++
155+
}
156+
for _, v := range nums2 {
157+
cnt2[v]++
158+
}
159+
for a, x := range cnt1 {
160+
for b, y := range cnt2 {
161+
if a*a%b == 0 {
162+
c := a * a / b
163+
if b == c {
164+
ans += x * y * (y - 1)
165+
} else {
166+
ans += x * y * cnt2[c]
167+
}
168+
}
169+
if b*b%a == 0 {
170+
c := b * b / a
171+
if a == c {
172+
ans += x * (x - 1) * y
173+
} else {
174+
ans += x * y * cnt1[c]
175+
}
176+
}
177+
}
178+
}
179+
ans /= 2
180+
return
181+
}
78182
```
79183

80184
### **...**

solution/1500-1599/1577.Number of Ways Where Square of Number Is Equal to Product of Two Numbers/README_EN.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,104 @@ Type 2: (3,0,1). nums2[3]<sup>2</sup> = nums1[0] * nums1[1].
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
60+
cnt1 = Counter(nums1)
61+
cnt2 = Counter(nums2)
62+
ans = 0
63+
for a, x in cnt1.items():
64+
for b, y in cnt2.items():
65+
if a * a % b == 0:
66+
c = a * a // b
67+
if b == c:
68+
ans += x * y * (y - 1)
69+
else:
70+
ans += x * y * cnt2[c]
71+
if b * b % a == 0:
72+
c = b * b // a
73+
if a == c:
74+
ans += x * (x - 1) * y
75+
else:
76+
ans += x * y * cnt1[c]
77+
return ans >> 1
5978
```
6079

6180
### **Java**
6281

6382
```java
83+
class Solution {
84+
public int numTriplets(int[] nums1, int[] nums2) {
85+
Map<Integer, Integer> cnt1 = new HashMap<>();
86+
Map<Integer, Integer> cnt2 = new HashMap<>();
87+
for (int v : nums1) {
88+
cnt1.put(v, cnt1.getOrDefault(v, 0) + 1);
89+
}
90+
for (int v : nums2) {
91+
cnt2.put(v, cnt2.getOrDefault(v, 0) + 1);
92+
}
93+
long ans = 0;
94+
for (var e1 : cnt1.entrySet()) {
95+
long a = e1.getKey(), x = e1.getValue();
96+
for (var e2 : cnt2.entrySet()) {
97+
long b = e2.getKey(), y = e2.getValue();
98+
if ((a * a) % b == 0) {
99+
long c = a * a / b;
100+
if (b == c) {
101+
ans += x * y * (y - 1);
102+
} else {
103+
ans += x * y * cnt2.getOrDefault((int) c, 0);
104+
}
105+
}
106+
if ((b * b) % a == 0) {
107+
long c = b * b / a;
108+
if (a == c) {
109+
ans += x * (x - 1) * y;
110+
} else {
111+
ans += x * y * cnt1.getOrDefault((int) c, 0);
112+
}
113+
}
114+
}
115+
}
116+
return (int) (ans >> 1);
117+
}
118+
}
119+
```
64120

121+
### **Go**
122+
123+
```go
124+
func numTriplets(nums1 []int, nums2 []int) (ans int) {
125+
cnt1 := map[int]int{}
126+
cnt2 := map[int]int{}
127+
for _, v := range nums1 {
128+
cnt1[v]++
129+
}
130+
for _, v := range nums2 {
131+
cnt2[v]++
132+
}
133+
for a, x := range cnt1 {
134+
for b, y := range cnt2 {
135+
if a*a%b == 0 {
136+
c := a * a / b
137+
if b == c {
138+
ans += x * y * (y - 1)
139+
} else {
140+
ans += x * y * cnt2[c]
141+
}
142+
}
143+
if b*b%a == 0 {
144+
c := b * b / a
145+
if a == c {
146+
ans += x * (x - 1) * y
147+
} else {
148+
ans += x * y * cnt1[c]
149+
}
150+
}
151+
}
152+
}
153+
ans /= 2
154+
return
155+
}
65156
```
66157

67158
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func numTriplets(nums1 []int, nums2 []int) (ans int) {
2+
cnt1 := map[int]int{}
3+
cnt2 := map[int]int{}
4+
for _, v := range nums1 {
5+
cnt1[v]++
6+
}
7+
for _, v := range nums2 {
8+
cnt2[v]++
9+
}
10+
for a, x := range cnt1 {
11+
for b, y := range cnt2 {
12+
if a*a%b == 0 {
13+
c := a * a / b
14+
if b == c {
15+
ans += x * y * (y - 1)
16+
} else {
17+
ans += x * y * cnt2[c]
18+
}
19+
}
20+
if b*b%a == 0 {
21+
c := b * b / a
22+
if a == c {
23+
ans += x * (x - 1) * y
24+
} else {
25+
ans += x * y * cnt1[c]
26+
}
27+
}
28+
}
29+
}
30+
ans /= 2
31+
return
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public int numTriplets(int[] nums1, int[] nums2) {
3+
Map<Integer, Integer> cnt1 = new HashMap<>();
4+
Map<Integer, Integer> cnt2 = new HashMap<>();
5+
for (int v : nums1) {
6+
cnt1.put(v, cnt1.getOrDefault(v, 0) + 1);
7+
}
8+
for (int v : nums2) {
9+
cnt2.put(v, cnt2.getOrDefault(v, 0) + 1);
10+
}
11+
long ans = 0;
12+
for (var e1 : cnt1.entrySet()) {
13+
long a = e1.getKey(), x = e1.getValue();
14+
for (var e2 : cnt2.entrySet()) {
15+
long b = e2.getKey(), y = e2.getValue();
16+
if ((a * a) % b == 0) {
17+
long c = a * a / b;
18+
if (b == c) {
19+
ans += x * y * (y - 1);
20+
} else {
21+
ans += x * y * cnt2.getOrDefault((int) c, 0);
22+
}
23+
}
24+
if ((b * b) % a == 0) {
25+
long c = b * b / a;
26+
if (a == c) {
27+
ans += x * (x - 1) * y;
28+
} else {
29+
ans += x * y * cnt1.getOrDefault((int) c, 0);
30+
}
31+
}
32+
}
33+
}
34+
return (int) (ans >> 1);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
3+
cnt1 = Counter(nums1)
4+
cnt2 = Counter(nums2)
5+
ans = 0
6+
for a, x in cnt1.items():
7+
for b, y in cnt2.items():
8+
if a * a % b == 0:
9+
c = a * a // b
10+
if b == c:
11+
ans += x * y * (y - 1)
12+
else:
13+
ans += x * y * cnt2[c]
14+
if b * b % a == 0:
15+
c = b * b // a
16+
if a == c:
17+
ans += x * (x - 1) * y
18+
else:
19+
ans += x * y * cnt1[c]
20+
return ans >> 1

0 commit comments

Comments
 (0)