avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

2341. How much can the array be formed One question daily

  2024-01-01        
字数统计: 201字   |   阅读时长: 1min

topic:

2023-02-16.png
2341. How much can the array be formed.md

Thought:

I am:

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。
Sort the list,Whether two or two are equal to whether。

Hash tableThought:

CounterAfter counting,a+=v//2,b+=v%2For each number x,
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,
You can get the current number x Number pairs that can be formed,Then we accumulate this number to the variable s middle。

Code:

Ordinary count
1
2
3
4
5
6
7
8
9
10
class Solution:
def numberOfPairs(self, nums: List[int]) -> List[int]:
nums.sort()
ans = [0, len(nums)]
for index in range(1, len(nums)):
if nums[index - 1] == nums[index]:
ans[0] += 1
ans[1] -= 2
nums[index - 1] = nums[index] = -1
return ans
Hash table
1
2
3
4
5
6
7
8
9
class Solution:
def numberOfPairs(self, nums: List[int]) -> List[int]:
x = Counter(nums)
a = 0
b = 0
for k,v in x.items():
a+=v//2
b+=v%2
return [a,b]
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
3072. Allocate elements into two arrays II
350.The intersection of two array
目录
  1. 1. topic:
  2. 2. Thought:
    1. 2.1. I am:
    2. 2.2. Hash tableThought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号