返回介绍

solution / 3000-3099 / 3002.Maximum Size of a Set After Removals / README_EN

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

3002. Maximum Size of a Set After Removals

中文文档

Description

You are given two 0-indexed integer arrays nums1 and nums2 of even length n.

You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.

Return _the maximum possible size of the set_ s.

 

Example 1:

Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
Output: 2
Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
It can be shown that 2 is the maximum possible size of the set s after the removals.

Example 2:

Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
Output: 5
Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
It can be shown that 5 is the maximum possible size of the set s after the removals.

Example 3:

Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
Output: 6
Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
It can be shown that 6 is the maximum possible size of the set s after the removals.

 

Constraints:

  • n == nums1.length == nums2.length
  • 1 <= n <= 2 * 104
  • n is even.
  • 1 <= nums1[i], nums2[i] <= 109

Solutions

Solution 1

class Solution:
  def maximumSetSize(self, nums1: List[int], nums2: List[int]) -> int:
    s1 = set(nums1)
    s2 = set(nums2)
    n = len(nums1)
    a = min(len(s1 - s2), n // 2)
    b = min(len(s2 - s1), n // 2)
    return min(a + b + len(s1 & s2), n)
class Solution {
  public int maximumSetSize(int[] nums1, int[] nums2) {
    Set<Integer> s1 = new HashSet<>();
    Set<Integer> s2 = new HashSet<>();
    for (int x : nums1) {
      s1.add(x);
    }
    for (int x : nums2) {
      s2.add(x);
    }
    int n = nums1.length;
    int a = 0, b = 0, c = 0;
    for (int x : s1) {
      if (!s2.contains(x)) {
        ++a;
      }
    }
    for (int x : s2) {
      if (!s1.contains(x)) {
        ++b;
      } else {
        ++c;
      }
    }
    a = Math.min(a, n / 2);
    b = Math.min(b, n / 2);
    return Math.min(a + b + c, n);
  }
}
class Solution {
public:
  int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
    unordered_set<int> s1(nums1.begin(), nums1.end());
    unordered_set<int> s2(nums2.begin(), nums2.end());
    int n = nums1.size();
    int a = 0, b = 0, c = 0;
    for (int x : s1) {
      if (!s2.count(x)) {
        ++a;
      }
    }
    for (int x : s2) {
      if (!s1.count(x)) {
        ++b;
      } else {
        ++c;
      }
    }
    a = min(a, n / 2);
    b = min(b, n / 2);
    return min(a + b + c, n);
  }
};
func maximumSetSize(nums1 []int, nums2 []int) int {
  s1 := map[int]bool{}
  s2 := map[int]bool{}
  for _, x := range nums1 {
    s1[x] = true
  }
  for _, x := range nums2 {
    s2[x] = true
  }
  a, b, c := 0, 0, 0
  for x := range s1 {
    if !s2[x] {
      a++
    }
  }
  for x := range s2 {
    if !s1[x] {
      b++
    } else {
      c++
    }
  }
  n := len(nums1)
  a = min(a, n/2)
  b = min(b, n/2)
  return min(a+b+c, n)
}
function maximumSetSize(nums1: number[], nums2: number[]): number {
  const s1: Set<number> = new Set(nums1);
  const s2: Set<number> = new Set(nums2);
  const n = nums1.length;
  let [a, b, c] = [0, 0, 0];
  for (const x of s1) {
    if (!s2.has(x)) {
      ++a;
    }
  }
  for (const x of s2) {
    if (!s1.has(x)) {
      ++b;
    } else {
      ++c;
    }
  }
  a = Math.min(a, n >> 1);
  b = Math.min(b, n >> 1);
  return Math.min(a + b + c, n);
}

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

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

发布评论

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