diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/README.md b/solution/2900-2999/2964.Number of Divisible Triplet Sums/README.md index 0c42c6325d804..7fd9ecb5ab347 100644 --- a/solution/2900-2999/2964.Number of Divisible Triplet Sums/README.md +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/README.md @@ -47,6 +47,12 @@ 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$ 的长度。 + ### **Python3** @@ -54,7 +60,16 @@ It can be shown that no other triplet is divisible by 5. Hence, the answer is 3. ```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** @@ -62,19 +77,75 @@ It can be shown that no other triplet is divisible by 5. Hence, the answer is 3. ```java - +class Solution { + public int divisibleTripletCount(int[] nums, int d) { + Map 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& nums, int d) { + unordered_map 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 = 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; +} ``` ### **...** diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/README_EN.md b/solution/2900-2999/2964.Number of Divisible Triplet Sums/README_EN.md index 746ef2543344a..fd82dd968cb8a 100644 --- a/solution/2900-2999/2964.Number of Divisible Triplet Sums/README_EN.md +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/README_EN.md @@ -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$. + ### **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 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& nums, int d) { + unordered_map 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 = 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; +} ``` ### **...** diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.cpp b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.cpp new file mode 100644 index 0000000000000..391d2539d89f0 --- /dev/null +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int divisibleTripletCount(vector& nums, int d) { + unordered_map 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; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.go b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.go new file mode 100644 index 0000000000000..fbac7af154d23 --- /dev/null +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.java b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.java new file mode 100644 index 0000000000000..5a6e3883e7b5c --- /dev/null +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int divisibleTripletCount(int[] nums, int d) { + Map 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; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.py b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.py new file mode 100644 index 0000000000000..07436b2ea1c28 --- /dev/null +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.py @@ -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 diff --git a/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.ts b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.ts new file mode 100644 index 0000000000000..e675670627e58 --- /dev/null +++ b/solution/2900-2999/2964.Number of Divisible Triplet Sums/Solution.ts @@ -0,0 +1,13 @@ +function divisibleTripletCount(nums: number[], d: number): number { + const n = nums.length; + const cnt: Map = 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 によって変換されたページ (->オリジナル) /