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 df32703

Browse files
feat: add solutions to lc problem: No.2964 (#2098)
No.2964.Number of Divisible Triplet Sums
1 parent 3422ef3 commit df32703

File tree

7 files changed

+212
-6
lines changed

7 files changed

+212
-6
lines changed

‎solution/2900-2999/2964.Number of Divisible Triplet Sums/README.md‎

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,105 @@ It can be shown that no other triplet is divisible by 5. Hence, the answer is 3.
4747

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

50+
**方法一:哈希表 + 枚举**
51+
52+
我们可以用哈希表 $cnt$ 记录 $nums[i] \bmod d$ 出现的次数,然后枚举 $j$ 和 $k,ドル计算使得等式 $(nums[i] + nums[j] + nums[k]) \bmod d = 0$ 成立的 $nums[i] \bmod d$ 的值,即 $(d - (nums[j] + nums[k]) \bmod d) \bmod d,ドル并将其出现次数累加到答案中。然后我们将 $nums[j] \bmod d$ 的出现次数加一。继续枚举 $j$ 和 $k,ドル直到 $j$ 到达数组末尾。
53+
54+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

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

5662
```python
57-
63+
class Solution:
64+
def divisibleTripletCount(self, nums: List[int], d: int) -> int:
65+
cnt = defaultdict(int)
66+
ans, n = 0, len(nums)
67+
for j in range(n):
68+
for k in range(j + 1, n):
69+
x = (d - (nums[j] + nums[k]) % d) % d
70+
ans += cnt[x]
71+
cnt[nums[j] % d] += 1
72+
return ans
5873
```
5974

6075
### **Java**
6176

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

6479
```java
65-
80+
class Solution {
81+
public int divisibleTripletCount(int[] nums, int d) {
82+
Map<Integer, Integer> cnt = new HashMap<>();
83+
int ans = 0, n = nums.length;
84+
for (int j = 0; j < n; ++j) {
85+
for (int k = j + 1; k < n; ++k) {
86+
int x = (d - (nums[j] + nums[k]) % d) % d;
87+
ans += cnt.getOrDefault(x, 0);
88+
}
89+
cnt.merge(nums[j] % d, 1, Integer::sum);
90+
}
91+
return ans;
92+
}
93+
}
6694
```
6795

6896
### **C++**
6997

7098
```cpp
71-
99+
class Solution {
100+
public:
101+
int divisibleTripletCount(vector<int>& nums, int d) {
102+
unordered_map<int, int> cnt;
103+
int ans = 0, n = nums.size();
104+
for (int j = 0; j < n; ++j) {
105+
for (int k = j + 1; k < n; ++k) {
106+
int x = (d - (nums[j] + nums[k]) % d) % d;
107+
ans += cnt[x];
108+
}
109+
cnt[nums[j] % d]++;
110+
}
111+
return ans;
112+
}
113+
};
72114
```
73115
74116
### **Go**
75117
76118
```go
119+
func divisibleTripletCount(nums []int, d int) (ans int) {
120+
n := len(nums)
121+
cnt := map[int]int{}
122+
for j := 0; j < n; j++ {
123+
for k := j + 1; k < n; k++ {
124+
x := (d - (nums[j]+nums[k])%d) % d
125+
ans += cnt[x]
126+
}
127+
cnt[nums[j]%d]++
128+
}
129+
return
130+
}
131+
```
77132

133+
### **TypeScript**
134+
135+
```ts
136+
function divisibleTripletCount(nums: number[], d: number): number {
137+
const n = nums.length;
138+
const cnt: Map<number, number> = new Map();
139+
let ans = 0;
140+
for (let j = 0; j < n; ++j) {
141+
for (let k = j + 1; k < n; ++k) {
142+
const x = (d - ((nums[j] + nums[k]) % d)) % d;
143+
ans += cnt.get(x) || 0;
144+
}
145+
cnt.set(nums[j] % d, (cnt.get(nums[j] % d) || 0) + 1);
146+
}
147+
return ans;
148+
}
78149
```
79150

80151
### **...**

‎solution/2900-2999/2964.Number of Divisible Triplet Sums/README_EN.md‎

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,101 @@ It can be shown that no other triplet is divisible by 5. Hence, the answer is 3.
4343

4444
## Solutions
4545

46+
**Solution 1: Hash Table + Enumeration**
47+
48+
We can use a hash table $cnt$ to record the occurrence times of $nums[i] \bmod d,ドル then enumerate $j$ and $k,ドル calculate the value of $nums[i] \bmod d$ that makes the equation $(nums[i] + nums[j] + nums[k]) \bmod d = 0$ hold, which is $(d - (nums[j] + nums[k]) \bmod d) \bmod d,ドル and accumulate its occurrence times to the answer. Then we increase the occurrence times of $nums[j] \bmod d$ by one. Continue to enumerate $j$ and $k$ until $j$ reaches the end of the array.
49+
50+
The time complexity is $O(n^2),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**
4955

5056
```python
51-
57+
class Solution:
58+
def divisibleTripletCount(self, nums: List[int], d: int) -> int:
59+
cnt = defaultdict(int)
60+
ans, n = 0, len(nums)
61+
for j in range(n):
62+
for k in range(j + 1, n):
63+
x = (d - (nums[j] + nums[k]) % d) % d
64+
ans += cnt[x]
65+
cnt[nums[j] % d] += 1
66+
return ans
5267
```
5368

5469
### **Java**
5570

5671
```java
57-
72+
class Solution {
73+
public int divisibleTripletCount(int[] nums, int d) {
74+
Map<Integer, Integer> cnt = new HashMap<>();
75+
int ans = 0, n = nums.length;
76+
for (int j = 0; j < n; ++j) {
77+
for (int k = j + 1; k < n; ++k) {
78+
int x = (d - (nums[j] + nums[k]) % d) % d;
79+
ans += cnt.getOrDefault(x, 0);
80+
}
81+
cnt.merge(nums[j] % d, 1, Integer::sum);
82+
}
83+
return ans;
84+
}
85+
}
5886
```
5987

6088
### **C++**
6189

6290
```cpp
63-
91+
class Solution {
92+
public:
93+
int divisibleTripletCount(vector<int>& nums, int d) {
94+
unordered_map<int, int> cnt;
95+
int ans = 0, n = nums.size();
96+
for (int j = 0; j < n; ++j) {
97+
for (int k = j + 1; k < n; ++k) {
98+
int x = (d - (nums[j] + nums[k]) % d) % d;
99+
ans += cnt[x];
100+
}
101+
cnt[nums[j] % d]++;
102+
}
103+
return ans;
104+
}
105+
};
64106
```
65107
66108
### **Go**
67109
68110
```go
111+
func divisibleTripletCount(nums []int, d int) (ans int) {
112+
n := len(nums)
113+
cnt := map[int]int{}
114+
for j := 0; j < n; j++ {
115+
for k := j + 1; k < n; k++ {
116+
x := (d - (nums[j]+nums[k])%d) % d
117+
ans += cnt[x]
118+
}
119+
cnt[nums[j]%d]++
120+
}
121+
return
122+
}
123+
```
69124

125+
### **TypeScript**
126+
127+
```ts
128+
function divisibleTripletCount(nums: number[], d: number): number {
129+
const n = nums.length;
130+
const cnt: Map<number, number> = new Map();
131+
let ans = 0;
132+
for (let j = 0; j < n; ++j) {
133+
for (let k = j + 1; k < n; ++k) {
134+
const x = (d - ((nums[j] + nums[k]) % d)) % d;
135+
ans += cnt.get(x) || 0;
136+
}
137+
cnt.set(nums[j] % d, (cnt.get(nums[j] % d) || 0) + 1);
138+
}
139+
return ans;
140+
}
70141
```
71142

72143
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int divisibleTripletCount(vector<int>& nums, int d) {
4+
unordered_map<int, int> cnt;
5+
int ans = 0, n = nums.size();
6+
for (int j = 0; j < n; ++j) {
7+
for (int k = j + 1; k < n; ++k) {
8+
int x = (d - (nums[j] + nums[k]) % d) % d;
9+
ans += cnt[x];
10+
}
11+
cnt[nums[j] % d]++;
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func divisibleTripletCount(nums []int, d int) (ans int) {
2+
n := len(nums)
3+
cnt := map[int]int{}
4+
for j := 0; j < n; j++ {
5+
for k := j + 1; k < n; k++ {
6+
x := (d - (nums[j]+nums[k])%d) % d
7+
ans += cnt[x]
8+
}
9+
cnt[nums[j]%d]++
10+
}
11+
return
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int divisibleTripletCount(int[] nums, int d) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
int ans = 0, n = nums.length;
5+
for (int j = 0; j < n; ++j) {
6+
for (int k = j + 1; k < n; ++k) {
7+
int x = (d - (nums[j] + nums[k]) % d) % d;
8+
ans += cnt.getOrDefault(x, 0);
9+
}
10+
cnt.merge(nums[j] % d, 1, Integer::sum);
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def divisibleTripletCount(self, nums: List[int], d: int) -> int:
3+
cnt = defaultdict(int)
4+
ans, n = 0, len(nums)
5+
for j in range(n):
6+
for k in range(j + 1, n):
7+
x = (d - (nums[j] + nums[k]) % d) % d
8+
ans += cnt[x]
9+
cnt[nums[j] % d] += 1
10+
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function divisibleTripletCount(nums: number[], d: number): number {
2+
const n = nums.length;
3+
const cnt: Map<number, number> = new Map();
4+
let ans = 0;
5+
for (let j = 0; j < n; ++j) {
6+
for (let k = j + 1; k < n; ++k) {
7+
const x = (d - ((nums[j] + nums[k]) % d)) % d;
8+
ans += cnt.get(x) || 0;
9+
}
10+
cnt.set(nums[j] % d, (cnt.get(nums[j] % d) || 0) + 1);
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
(0)

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