954. Array of Doubled Pairs
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 39% Topics: Array, Hash Table, Greedy, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# 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
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions