Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c64265d

Browse files
feat: add solutions to lc problem: No.1726 (#1845)
No.1726.Tuple with Same Product
1 parent 12a84ea commit c64265d

File tree

6 files changed

+153
-25
lines changed

6 files changed

+153
-25
lines changed

‎solution/1700-1799/1726.Tuple with Same Product/README.md‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
**方法一:组合数+哈希表**
49+
**方法一:组合数 + 哈希表**
5050

51-
假设存在 `n` 组数,对于其中任意两组数 `a、b``c、d`,均满足 $a * b = c * d$ 的条件,则这样的组合一共有 $\mathrm{C}_n^2 = \frac{n*(n-1)}{2}$ 个。
51+
假设存在 $n$ 组数,对于其中任意两组数 $a, b$$c, d$,均满足 $a \times b = c \times d$ 的条件,则这样的组合一共有 $\mathrm{C}_n^2 = \frac{n \times (n-1)}{2}$ 个。
5252

53-
根据题意每一组满足上述条件的组合可以构成 `8` 个满足题意的元组,故将各个相同乘积的组合数乘以 `8` 相加即可得到结果
53+
根据题意每一组满足上述条件的组合可以构成 8ドル$ 个满足题意的元组,故将各个相同乘积的组合数乘以 8ドル$ 相加(等价于:左移 3ドル$ 位)即可得到结果
5454

55-
时间复杂度 $O(n^2),ドル空间复杂度 $O(n^2)$。
55+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n^2)$。其中 $n$ 为数组长度。
5656

5757
<!-- tabs:start -->
5858

@@ -82,7 +82,7 @@ class Solution {
8282
for (int i = 1; i < nums.length; ++i) {
8383
for (int j = 0; j < i; ++j) {
8484
int x = nums[i] * nums[j];
85-
cnt.put(x, cnt.getOrDefault(x, 0) +1);
85+
cnt.merge(x, 1, Integer::sum);
8686
}
8787
}
8888
int ans = 0;
@@ -135,6 +135,51 @@ func tupleSameProduct(nums []int) int {
135135
}
136136
```
137137

138+
### **Rust**
139+
140+
```rust
141+
use std::collections::HashMap;
142+
143+
impl Solution {
144+
pub fn tuple_same_product(nums: Vec<i32>) -> i32 {
145+
let mut cnt: HashMap<i32, i32> = HashMap::new();
146+
let mut ans = 0;
147+
148+
for i in 1..nums.len() {
149+
for j in 0..i {
150+
let x = nums[i] * nums[j];
151+
*cnt.entry(x).or_insert(0) += 1;
152+
}
153+
}
154+
155+
for v in cnt.values() {
156+
ans += v * (v - 1) / 2;
157+
}
158+
159+
ans << 3
160+
}
161+
}
162+
```
163+
164+
### **TypeScript**
165+
166+
```ts
167+
function tupleSameProduct(nums: number[]): number {
168+
const cnt: Map<number, number> = new Map();
169+
for (let i = 1; i < nums.length; ++i) {
170+
for (let j = 0; j < i; ++j) {
171+
const x = nums[i] * nums[j];
172+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
173+
}
174+
}
175+
let ans = 0;
176+
for (const [_, v] of cnt) {
177+
ans += (v * (v - 1)) / 2;
178+
}
179+
return ans << 3;
180+
}
181+
```
182+
138183
### **...**
139184

140185
```

‎solution/1700-1799/1726.Tuple with Same Product/README_EN.md‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040

4141
## Solutions
4242

43-
**Solution 1: Number of Combinations + Hash Table**
43+
**Solution 1: Combination + Hash Table**
4444

45-
Time complexity $O(n^2),ドル Space complexity $O(n^2)$.
45+
Assuming there are $n$ pairs of numbers, for any two pairs of numbers $a, b$ and $c, d$ that satisfy the condition $a \times b = c \times d,ドル there are a total of $\mathrm{C}_n^2 = \frac{n \times (n-1)}{2}$ such combinations.
46+
47+
According to the problem description, each combination that satisfies the above condition can form 8ドル$ tuples that satisfy the problem requirements. Therefore, we can multiply the number of combinations with the same product by 8ドル$ (equivalent to left shifting by 3ドル$ bits) and add them up to get the result.
48+
49+
The time complexity is $O(n^2),ドル and the space complexity is $O(n^2)$. Here, $n$ is the length of the array.
4650

4751
<!-- tabs:start -->
4852

@@ -68,7 +72,7 @@ class Solution {
6872
for (int i = 1; i < nums.length; ++i) {
6973
for (int j = 0; j < i; ++j) {
7074
int x = nums[i] * nums[j];
71-
cnt.put(x, cnt.getOrDefault(x, 0) +1);
75+
cnt.merge(x, 1, Integer::sum);
7276
}
7377
}
7478
int ans = 0;
@@ -121,6 +125,51 @@ func tupleSameProduct(nums []int) int {
121125
}
122126
```
123127

128+
### **Rust**
129+
130+
```rust
131+
use std::collections::HashMap;
132+
133+
impl Solution {
134+
pub fn tuple_same_product(nums: Vec<i32>) -> i32 {
135+
let mut cnt: HashMap<i32, i32> = HashMap::new();
136+
let mut ans = 0;
137+
138+
for i in 1..nums.len() {
139+
for j in 0..i {
140+
let x = nums[i] * nums[j];
141+
*cnt.entry(x).or_insert(0) += 1;
142+
}
143+
}
144+
145+
for v in cnt.values() {
146+
ans += v * (v - 1) / 2;
147+
}
148+
149+
ans << 3
150+
}
151+
}
152+
```
153+
154+
### **TypeScript**
155+
156+
```ts
157+
function tupleSameProduct(nums: number[]): number {
158+
const cnt: Map<number, number> = new Map();
159+
for (let i = 1; i < nums.length; ++i) {
160+
for (let j = 0; j < i; ++j) {
161+
const x = nums[i] * nums[j];
162+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
163+
}
164+
}
165+
let ans = 0;
166+
for (const [_, v] of cnt) {
167+
ans += (v * (v - 1)) / 2;
168+
}
169+
return ans << 3;
170+
}
171+
```
172+
124173
### **...**
125174

126175
```

‎solution/1700-1799/1726.Tuple with Same Product/Solution.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
func tupleSameProduct(nums []int) int {
1+
func tupleSameProduct(nums []int) (ansint) {
22
cnt := map[int]int{}
33
for i := 1; i < len(nums); i++ {
44
for j := 0; j < i; j++ {
55
x := nums[i] * nums[j]
66
cnt[x]++
77
}
88
}
9-
ans := 0
109
for _, v := range cnt {
1110
ans += v * (v - 1) / 2
1211
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
class Solution {
2-
public int tupleSameProduct(int[] nums) {
3-
Map<Integer, Integer> cnt = new HashMap<>();
4-
for (int i = 1; i < nums.length; ++i) {
5-
for (int j = 0; j < i; ++j) {
6-
int x = nums[i] * nums[j];
7-
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
8-
}
9-
}
10-
int ans = 0;
11-
for (int v : cnt.values()) {
12-
ans += v * (v - 1) / 2;
13-
}
14-
return ans << 3;
15-
}
1+
class Solution {
2+
public int tupleSameProduct(int[] nums) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
for (int i = 1; i < nums.length; ++i) {
5+
for (int j = 0; j < i; ++j) {
6+
int x = nums[i] * nums[j];
7+
cnt.merge(x, 1, Integer::sum);
8+
}
9+
}
10+
int ans = 0;
11+
for (int v : cnt.values()) {
12+
ans += v * (v - 1) / 2;
13+
}
14+
return ans << 3;
15+
}
1616
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn tuple_same_product(nums: Vec<i32>) -> i32 {
5+
let mut cnt: HashMap<i32, i32> = HashMap::new();
6+
let mut ans = 0;
7+
8+
for i in 1..nums.len() {
9+
for j in 0..i {
10+
let x = nums[i] * nums[j];
11+
*cnt.entry(x).or_insert(0) += 1;
12+
}
13+
}
14+
15+
for v in cnt.values() {
16+
ans += v * (v - 1) / 2;
17+
}
18+
19+
ans << 3
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function tupleSameProduct(nums: number[]): number {
2+
const cnt: Map<number, number> = new Map();
3+
for (let i = 1; i < nums.length; ++i) {
4+
for (let j = 0; j < i; ++j) {
5+
const x = nums[i] * nums[j];
6+
cnt.set(x, (cnt.get(x) ?? 0) + 1);
7+
}
8+
}
9+
let ans = 0;
10+
for (const [_, v] of cnt) {
11+
ans += (v * (v - 1)) / 2;
12+
}
13+
return ans << 3;
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /