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

feat: add solutions to lc problem: No.2964 #2098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
acbin merged 1 commit into main from dev
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 74 additions & 3 deletions solution/2900-2999/2964.Number of Divisible Triplet Sums/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,105 @@ It can be shown that no other triplet is divisible by 5. Hence, the answer is 3.

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

**方法一:哈希表 + 枚举**

我们可以用哈希表 $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$ 到达数组末尾。

时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def divisibleTripletCount(self, nums: List[int], d: int) -> int:
cnt = defaultdict(int)
ans, n = 0, len(nums)
for j in range(n):
for k in range(j + 1, n):
x = (d - (nums[j] + nums[k]) % d) % d
ans += cnt[x]
cnt[nums[j] % d] += 1
return ans
```

### **Java**

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

```java

class Solution {
public int divisibleTripletCount(int[] nums, int d) {
Map<Integer, Integer> cnt = new HashMap<>();
int ans = 0, n = nums.length;
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
int x = (d - (nums[j] + nums[k]) % d) % d;
ans += cnt.getOrDefault(x, 0);
}
cnt.merge(nums[j] % d, 1, Integer::sum);
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int divisibleTripletCount(vector<int>& nums, int d) {
unordered_map<int, int> cnt;
int ans = 0, n = nums.size();
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
int x = (d - (nums[j] + nums[k]) % d) % d;
ans += cnt[x];
}
cnt[nums[j] % d]++;
}
return ans;
}
};
```

### **Go**

```go
func divisibleTripletCount(nums []int, d int) (ans int) {
n := len(nums)
cnt := map[int]int{}
for j := 0; j < n; j++ {
for k := j + 1; k < n; k++ {
x := (d - (nums[j]+nums[k])%d) % d
ans += cnt[x]
}
cnt[nums[j]%d]++
}
return
}
```

### **TypeScript**

```ts
function divisibleTripletCount(nums: number[], d: number): number {
const n = nums.length;
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let j = 0; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
const x = (d - ((nums[j] + nums[k]) % d)) % d;
ans += cnt.get(x) || 0;
}
cnt.set(nums[j] % d, (cnt.get(nums[j] % d) || 0) + 1);
}
return ans;
}
```

### **...**
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,101 @@ It can be shown that no other triplet is divisible by 5. Hence, the answer is 3.

## Solutions

**Solution 1: Hash Table + Enumeration**

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.

The time complexity is $O(n^2),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def divisibleTripletCount(self, nums: List[int], d: int) -> int:
cnt = defaultdict(int)
ans, n = 0, len(nums)
for j in range(n):
for k in range(j + 1, n):
x = (d - (nums[j] + nums[k]) % d) % d
ans += cnt[x]
cnt[nums[j] % d] += 1
return ans
```

### **Java**

```java

class Solution {
public int divisibleTripletCount(int[] nums, int d) {
Map<Integer, Integer> cnt = new HashMap<>();
int ans = 0, n = nums.length;
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
int x = (d - (nums[j] + nums[k]) % d) % d;
ans += cnt.getOrDefault(x, 0);
}
cnt.merge(nums[j] % d, 1, Integer::sum);
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int divisibleTripletCount(vector<int>& nums, int d) {
unordered_map<int, int> cnt;
int ans = 0, n = nums.size();
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
int x = (d - (nums[j] + nums[k]) % d) % d;
ans += cnt[x];
}
cnt[nums[j] % d]++;
}
return ans;
}
};
```

### **Go**

```go
func divisibleTripletCount(nums []int, d int) (ans int) {
n := len(nums)
cnt := map[int]int{}
for j := 0; j < n; j++ {
for k := j + 1; k < n; k++ {
x := (d - (nums[j]+nums[k])%d) % d
ans += cnt[x]
}
cnt[nums[j]%d]++
}
return
}
```

### **TypeScript**

```ts
function divisibleTripletCount(nums: number[], d: number): number {
const n = nums.length;
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let j = 0; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
const x = (d - ((nums[j] + nums[k]) % d)) % d;
ans += cnt.get(x) || 0;
}
cnt.set(nums[j] % d, (cnt.get(nums[j] % d) || 0) + 1);
}
return ans;
}
```

### **...**
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int divisibleTripletCount(vector<int>& nums, int d) {
unordered_map<int, int> cnt;
int ans = 0, n = nums.size();
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
int x = (d - (nums[j] + nums[k]) % d) % d;
ans += cnt[x];
}
cnt[nums[j] % d]++;
}
return ans;
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func divisibleTripletCount(nums []int, d int) (ans int) {
n := len(nums)
cnt := map[int]int{}
for j := 0; j < n; j++ {
for k := j + 1; k < n; k++ {
x := (d - (nums[j]+nums[k])%d) % d
ans += cnt[x]
}
cnt[nums[j]%d]++
}
return
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int divisibleTripletCount(int[] nums, int d) {
Map<Integer, Integer> cnt = new HashMap<>();
int ans = 0, n = nums.length;
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
int x = (d - (nums[j] + nums[k]) % d) % d;
ans += cnt.getOrDefault(x, 0);
}
cnt.merge(nums[j] % d, 1, Integer::sum);
}
return ans;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def divisibleTripletCount(self, nums: List[int], d: int) -> int:
cnt = defaultdict(int)
ans, n = 0, len(nums)
for j in range(n):
for k in range(j + 1, n):
x = (d - (nums[j] + nums[k]) % d) % d
ans += cnt[x]
cnt[nums[j] % d] += 1
return ans
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function divisibleTripletCount(nums: number[], d: number): number {
const n = nums.length;
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let j = 0; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
const x = (d - ((nums[j] + nums[k]) % d)) % d;
ans += cnt.get(x) || 0;
}
cnt.set(nums[j] % d, (cnt.get(nums[j] % d) || 0) + 1);
}
return ans;
}

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