Skip to content

Commit 8216e64

Browse files
authored
Create find-original-array-from-doubled-array.py
1 parent ece1cea commit 8216e64

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(n + klogk), k is the distinct number of changed
2+
# Space: O(k)
3+
4+
class Solution(object):
5+
def findOriginalArray(self, changed):
6+
"""
7+
:type changed: List[int]
8+
:rtype: List[int]
9+
"""
10+
if len(changed)%2:
11+
return []
12+
cnts = collections.Counter(changed)
13+
for x in sorted(cnts.iterkeys()):
14+
if cnts[x] > cnts[2*x]:
15+
return []
16+
cnts[2*x] -= cnts[x] if x else cnts[x]//2
17+
return list(cnts.elements())

0 commit comments

Comments
 (0)