Skip to main content

2007 - Find Original Array From Doubled Array

Details

KeyValue
Linkhttps://leetcode.com/problems/find-original-array-from-doubled-array/
LanguagePython 3
Runtime2406 ms, faster than 42.32% of Python3 online submissions for Find Original Array From Doubled Array
Memory Usage32.8 MB, less than 33.24% of Python3 online submissions for Find Original Array From Doubled Array
DatastructuresCounter
AlgorithmsGreedy + Counter
ComplexityTime: O(KlogK) Memory: O( N) (K=number of distinct keys in Counter hash)

Procedure

  1. ...

Code

class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
count_of = collections.Counter(changed)

# If we have zeros, ensure we have even number of them
if count_of[0] % 2: return []

for num in sorted(count_of):
if count_of[num] > count_of[2*num]: return []
if num: count_of[2*num] -= count_of[num]
else: count_of[2*num] = count_of[num] // 2

return list(count_of.elements())