Skip to content

Commit 14de502

Browse files
committed
chore: add daily leetcode post 2341. 数组能形成多少数对_translated
1 parent 3c4fbac commit 14de502

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: 2341. How much can the array be formed One question daily
3+
tags:
4+
- - Python
5+
- - answer
6+
abbrlink: f953c753
7+
date: '2024.01.01 0:00'
8+
---
9+
10+
# topic:
11+
12+
[2341. How much can the array be formed.md](https://leetcode.cn/problems/maximum-number-of-pairs-in-array/description/)
13+
14+
# Thought:
15+
## I am:
16+
I don't know if I saw it“Simple”Two words,This question has the initiative to think about the optimal solution。Actually this timeylbBig guy's hash table method is still fast。
17+
Sort the list,Whether two or two are equal to whether。
18+
## Hash tableThought:
19+
CounterAfter counting,`a+=v//2`,`b+=v%2`For each number x,
20+
if x Number of times v more than the 1,You can choose two from the array x Form a number pair,we willv Divide 2 Take down,
21+
You can get the current number x Number pairs that can be formed,Then we accumulate this number to the variable s middle。
22+
23+
# Code:
24+
25+
```python Ordinary count
26+
class Solution:
27+
def numberOfPairs(self, nums: List[int]) -> List[int]:
28+
nums.sort()
29+
ans = [0, len(nums)]
30+
for index in range(1, len(nums)):
31+
if nums[index - 1] == nums[index]:
32+
ans[0] += 1
33+
ans[1] -= 2
34+
nums[index - 1] = nums[index] = -1
35+
return ans
36+
```
37+
```python Hash table
38+
class Solution:
39+
def numberOfPairs(self, nums: List[int]) -> List[int]:
40+
x = Counter(nums)
41+
a = 0
42+
b = 0
43+
for k,v in x.items():
44+
a+=v//2
45+
b+=v%2
46+
return [a,b]
47+
```

0 commit comments

Comments
 (0)