Array of Doubled Pairs LeetCode solution in Python
1# Runtime: 1769 ms (Top 5.17%) | Memory: 16.6 MB (Top 56.95%)2class Solution:3def canReorderDoubled(self, arr: List[int]) -> bool:4n = len(arr)5arr.sort()6times = n // 27count = {}8for i in arr:9if i in count:10count[i] += 111else:12count[i] = 113for i in count:14if i == 0:15tmp = count[0] // 216times -= tmp17if times <= 0:18return True19else:20if i * 2 in count:21ct1 = count[i]22ct2 = count[i * 2]23while ct1 > 0 and ct2 > 0 and times > 0:24ct1 -= 125ct2 -= 126times -= 127count[i] = ct128count[i * 2] = ct229if times == 0:30return True31return False