返回介绍

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

发布于 2024-06-17 01:02:58 字数 4553 浏览 0 评论 0 收藏 0

2964. Number of Divisible Triplet Sums

中文文档

Description

Given a 0-indexed integer array nums and an integer d, return _the number of triplets_ (i, j, k) _such that_ i < j < k _and_ (nums[i] + nums[j] + nums[k]) % d == 0.

 

Example 1:

Input: nums = [3,3,4,7,8], d = 5
Output: 3
Explanation: The triplets which are divisible by 5 are: (0, 1, 2), (0, 2, 4), (1, 2, 4).
It can be shown that no other triplet is divisible by 5. Hence, the answer is 3.

Example 2:

Input: nums = [3,3,3,3], d = 3
Output: 4
Explanation: Any triplet chosen here has a sum of 9, which is divisible by 3. Hence, the answer is the total number of triplets which is 4.

Example 3:

Input: nums = [3,3,3,3], d = 6
Output: 0
Explanation: Any triplet chosen here has a sum of 9, which is not divisible by 6. Hence, the answer is 0.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 109
  • 1 <= d <= 109

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$.

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
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;
  }
}
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;
  }
};
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
}
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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文