返回介绍

solution / 2300-2399 / 2341.Maximum Number of Pairs in Array / README_EN

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

2341. Maximum Number of Pairs in Array

中文文档

Description

You are given a 0-indexed integer array nums. In one operation, you may do the following:

  • Choose two integers in nums that are equal.
  • Remove both integers from nums, forming a pair.

The operation is done on nums as many times as possible.

Return _a 0-indexed integer array _answer_ of size _2_ where _answer[0]_ is the number of pairs that are formed and _answer[1]_ is the number of leftover integers in _nums_ after doing the operation as many times as possible_.

 

Example 1:

Input: nums = [1,3,2,1,3,2,2]
Output: [3,1]
Explanation:
Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.

Example 2:

Input: nums = [1,1]
Output: [1,0]
Explanation: Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.

Example 3:

Input: nums = [0]
Output: [0,1]
Explanation: No pairs can be formed, and there is 1 number leftover in nums.

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

Solutions

Solution 1

class Solution:
  def numberOfPairs(self, nums: List[int]) -> List[int]:
    cnt = Counter(nums)
    s = sum(v // 2 for v in cnt.values())
    return [s, len(nums) - s * 2]
class Solution {
  public int[] numberOfPairs(int[] nums) {
    int[] cnt = new int[101];
    for (int x : nums) {
      ++cnt[x];
    }
    int s = 0;
    for (int v : cnt) {
      s += v / 2;
    }
    return new int[] {s, nums.length - s * 2};
  }
}
class Solution {
public:
  vector<int> numberOfPairs(vector<int>& nums) {
    vector<int> cnt(101);
    for (int& x : nums) {
      ++cnt[x];
    }
    int s = 0;
    for (int& v : cnt) {
      s += v >> 1;
    }
    return {s, (int) nums.size() - s * 2};
  }
};
func numberOfPairs(nums []int) []int {
  cnt := [101]int{}
  for _, x := range nums {
    cnt[x]++
  }
  s := 0
  for _, v := range cnt {
    s += v / 2
  }
  return []int{s, len(nums) - s*2}
}
function numberOfPairs(nums: number[]): number[] {
  const n = nums.length;
  const count = new Array(101).fill(0);
  for (const num of nums) {
    count[num]++;
  }
  const sum = count.reduce((r, v) => r + (v >> 1), 0);
  return [sum, n - sum * 2];
}
impl Solution {
  pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
    let n = nums.len();
    let mut count = [0; 101];
    for &v in nums.iter() {
      count[v as usize] += 1;
    }
    let mut sum = 0;
    for v in count.iter() {
      sum += v >> 1;
    }
    vec![sum as i32, (n - sum * 2) as i32]
  }
}
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var numberOfPairs = function (nums) {
  const cnt = new Array(101).fill(0);
  for (const x of nums) {
    ++cnt[x];
  }
  const s = cnt.reduce((a, b) => a + (b >> 1), 0);
  return [s, nums.length - s * 2];
};
public class Solution {
  public int[] NumberOfPairs(int[] nums) {
    int[] cnt = new int[101];
    foreach(int x in nums) {
      ++cnt[x];
    }
    int s = 0;
    foreach(int v in cnt) {
      s += v / 2;
    }
    return new int[] {s, nums.Length - s * 2};
  }
}
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* numberOfPairs(int* nums, int numsSize, int* returnSize) {
  int count[101] = {0};
  for (int i = 0; i < numsSize; i++) {
    count[nums[i]]++;
  }
  int sum = 0;
  for (int i = 0; i < 101; i++) {
    sum += count[i] >> 1;
  }
  int* ans = malloc(sizeof(int) * 2);
  ans[0] = sum;
  ans[1] = numsSize - sum * 2;
  *returnSize = 2;
  return ans;
}

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

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

发布评论

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