-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.py
60 lines (47 loc) · 1.48 KB
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
An integer array original is transformed into a doubled array changed by appending twice the value of every element in original, and then randomly shuffling the resulting array.
Given an array changed, return original if changed is a doubled array. If changed is not a doubled array, return an empty array. The elements in original may be returned in any order.
Example 1:
Input: changed = [1,3,4,2,6,8]
Output: [1,3,4]
Explanation: One possible original array could be [1,3,4]:
- Twice the value of 1 is 1 * 2 = 2.
- Twice the value of 3 is 3 * 2 = 6.
- Twice the value of 4 is 4 * 2 = 8.
Other original arrays could be [4,3,1] or [3,1,4].
Example 2:
Input: changed = [6,3,0,1]
Output: []
Explanation: changed is not a doubled array.
Example 3:
Input: changed = [1]
Output: []
Explanation: changed is not a doubled array.
Constraints:
1 <= changed.length <= 105
0 <= changed[i] <= 105
"""
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 != 0:
return []
ret = []
cnt = Counter(changed)
sks = list(sorted(cnt.keys()))
i = 0
while i < len(sks):
a = sks[i]
if cnt[a] == 0:
i += 1
continue
a2 = a + a
if cnt.get(a2, 0) <= 0:
return []
ret.append(a)
cnt[a] -= 1
cnt[a2] -= 1
if cnt[a] == 0:
i += 1
return ret