Array of Doubled Pairs LeetCode solution in Python

1
# Runtime: 1769 ms (Top 5.17%) | Memory: 16.6 MB (Top 56.95%)
2
class Solution:
3
def canReorderDoubled(self, arr: List[int]) -> bool:
4
n = len(arr)
5
arr.sort()
6
times = n // 2
7
count = {}
8
for i in arr:
9
if i in count:
10
count[i] += 1
11
else:
12
count[i] = 1
13
for i in count:
14
if i == 0:
15
tmp = count[0] // 2
16
times -= tmp
17
if times <= 0:
18
return True
19
else:
20
if i * 2 in count:
21
ct1 = count[i]
22
ct2 = count[i * 2]
23
while ct1 > 0 and ct2 > 0 and times > 0:
24
ct1 -= 1
25
ct2 -= 1
26
times -= 1
27
count[i] = ct1
28
count[i * 2] = ct2
29
if times == 0:
30
return True
31
return False

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0