返回介绍

solution / 2300-2399 / 2354.Number of Excellent Pairs / README_EN

发布于 2024-06-17 01:03:07 字数 4651 浏览 0 评论 0 收藏 0

2354. Number of Excellent Pairs

中文文档

Description

You are given a 0-indexed positive integer array nums and a positive integer k.

A pair of numbers (num1, num2) is called excellent if the following conditions are satisfied:

  • Both the numbers num1 and num2 exist in the array nums.
  • The sum of the number of set bits in num1 OR num2 and num1 AND num2 is greater than or equal to k, where OR is the bitwise OR operation and AND is the bitwise AND operation.

Return _the number of distinct excellent pairs_.

Two pairs (a, b) and (c, d) are considered distinct if either a != c or b != d. For example, (1, 2) and (2, 1) are distinct.

Note that a pair (num1, num2) such that num1 == num2 can also be excellent if you have at least one occurrence of num1 in the array.

 

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: 5
Explanation: The excellent pairs are the following:
- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
So the number of excellent pairs is 5.

Example 2:

Input: nums = [5,1,1], k = 10
Output: 0
Explanation: There are no excellent pairs for this array.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 1 <= k <= 60

Solutions

Solution 1

class Solution:
  def countExcellentPairs(self, nums: List[int], k: int) -> int:
    s = set(nums)
    ans = 0
    cnt = Counter()
    for v in s:
      cnt[v.bit_count()] += 1
    for v in s:
      t = v.bit_count()
      for i, x in cnt.items():
        if t + i >= k:
          ans += x
    return ans
class Solution {
  public long countExcellentPairs(int[] nums, int k) {
    Set<Integer> s = new HashSet<>();
    for (int v : nums) {
      s.add(v);
    }
    long ans = 0;
    int[] cnt = new int[32];
    for (int v : s) {
      int t = Integer.bitCount(v);
      ++cnt[t];
    }
    for (int v : s) {
      int t = Integer.bitCount(v);
      for (int i = 0; i < 32; ++i) {
        if (t + i >= k) {
          ans += cnt[i];
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  long long countExcellentPairs(vector<int>& nums, int k) {
    unordered_set<int> s(nums.begin(), nums.end());
    vector<int> cnt(32);
    for (int v : s) ++cnt[__builtin_popcount(v)];
    long long ans = 0;
    for (int v : s) {
      int t = __builtin_popcount(v);
      for (int i = 0; i < 32; ++i) {
        if (t + i >= k) {
          ans += cnt[i];
        }
      }
    }
    return ans;
  }
};
func countExcellentPairs(nums []int, k int) int64 {
  s := map[int]bool{}
  for _, v := range nums {
    s[v] = true
  }
  cnt := make([]int, 32)
  for v := range s {
    t := bits.OnesCount(uint(v))
    cnt[t]++
  }
  ans := 0
  for v := range s {
    t := bits.OnesCount(uint(v))
    for i, x := range cnt {
      if t+i >= k {
        ans += x
      }
    }
  }
  return int64(ans)
}

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

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

发布评论

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