首页 > 其他分享 >[LeetCode] 2341. Maximum Number of Pairs in Array

[LeetCode] 2341. Maximum Number of Pairs in Array

时间:2023-02-16 01:33:05浏览次数:43  
标签:Pairs 2341 nums int formed Number answer pair pairs

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

数组能形成多少数对。

给你一个下标从 0 开始的整数数组 nums 。在一步操作中,你可以执行以下步骤:

从 nums 选出 两个 相等的 整数
从 nums 中移除这两个整数,形成一个 数对
请你在 nums 上多次执行此操作直到无法继续执行。

返回一个下标从 0 开始、长度为 2 的整数数组 answer 作为答案,其中 answer[0] 是形成的数对数目,answer[1] 是对 nums 尽可能执行上述操作后剩下的整数数目。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-number-of-pairs-in-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是 counting sort。需要遍历两次 input 数组,第一次遍历我们用一个哈希表把每个不同元素的出现次数记录下来;第二次遍历的时候,遍历哈希表的 keyset,对于每个不同的 key,如果他的出现次数 % 2 == 0,则他是一个 pair;否则他最后一定有一个落单的。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int[] numberOfPairs(int[] nums) {
 3         int pair = 0;
 4         int leftOver = 0;
 5         HashMap<Integer, Integer> map = new HashMap<>();
 6         for (int num : nums) {
 7             map.put(num, map.getOrDefault(num, 0) + 1);
 8         }
 9         
10         for (int num : map.keySet()) {
11             int val = map.get(num);
12             if (val % 2 == 1) {
13                 leftOver++;
14             }
15             pair += val / 2;
16         }
17         
18         int[] res = new int[2];
19         res[0] = pair;
20         res[1] = leftOver;
21         return res;
22     }
23 }

 

LeetCode 题目总结

标签:Pairs,2341,nums,int,formed,Number,answer,pair,pairs
From: https://www.cnblogs.com/cnoodle/p/17125284.html

相关文章

  • [ABC262D] I Hate Non-integer Number 题解
    [ABC262D]IHateNon-integerNumberSolution目录[ABC262D]IHateNon-integerNumberSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入......
  • Pseudoprime numbers(POJ-3641 快速幂)
    快速幂:快速幂就是所求的幂次方过大,导致代码所用的时间超限。如:求2^3,3的二进制是11,(n&1)判断次方数的二进制是否为1,n>>1,向右进位1:代码:k=1,t=n;while(n){if(n&1)//......
  • 去掉Element 中el-input type=number时尾部上下箭头、禁用鼠标滚动
    在Element中,如果我们的输入框中需要输入数字时,将el-input的类型设置为number,这时输入框的尾部会出现上下箭头,影响美观性,这时只需在页面中加入如下样式,就可以去掉输入框尾部......
  • CF446C DZY Loves Fibonacci Numbers 题解和加强
    简要题意https://www.luogu.com.cn/problem/CF446C给定一个长度为\(n\)的序列\(A\),要求支持两种操作:1给定区间\((l,r)\)对这个区间内的每个数,依次加斐波那契数列......
  • [JavaScript]内置对象Number初识
    学习:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/NumberNumber类型可以表示整型和浮点型。123===123.0;//trueNumber......
  • Hidden Number Problem
    在最近的很多比赛都遇到了这个HiddenNumberProblem(HNP),所以抽个时间来仔细学习一下,然后马上要HGAME2023了,正好准备一下题目给新生写。IntroduceHNP问题第一次被提出......
  • Count the Number of Fair Pairs
    CounttheNumberofFairPairsGivena0-indexed integerarray nums ofsize n andtwointegers lower and upper ,returnthenumberoffairpairs.Apa......
  • B. Sum of Two Numbers
    B.SumofTwoNumbersThesumofdigitsofanon-negativeinteger$a$istheresultofsummingupitsdigitstogetherwhenwritteninthedecimalsystem.Fore......
  • Vue 收集表单数据-输入input,单选radio,多选checkbox,下拉框select ,以及v-model的3个
    From案例分析:1、Html部分:<[email protected]=""style="border:1pxsolidrgb(109,200,253);background-color:aliceblue;padding:8px;margin:8px;......
  • B. Sum of Two Numbers
    B.SumofTwoNumbershttps://codeforces.com/problemset/problem/1788/B 思路计算原数的所有位置上的digit和得到digit和的一半 对于原数从左到右切分,前半部分d......