Medium
Array of Doubled Pairs — Python
Full explanation · Time O(n + klogk) · Space O(k)
# Time: O(n + klogk)
# Space: O(k)
import collections
class Solution(object):
def canReorderDoubled(self, A):
"""
:type A: List[int]
:rtype: bool
"""
count = collections.Counter(A)
for x in sorted(count, key=abs):
if count[x] > count[2*x]:
return False
count[2*x] -= count[x]
return True