-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_1982.py
31 lines (25 loc) · 937 Bytes
/
LC_1982.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
from collections import Counter
from typing import List
class Solution:
def recoverArray(self, n: int, sums: List[int]) -> List[int]:
result = []
sums.sort()
while len(sums) > 1:
num = sums[-1] - sums[-2]
count_map = Counter(sums) # Get count of each element
excluding = [] # Subset sums that do NOT contain num
including = [] # Subset sums that contain num
for x in sums:
if count_map.get(x) > 0:
excluding.append(x)
including.append(x + num)
count_map[x] -= 1
count_map[x + num] -= 1
# Check validity of excluding set
if 0 in excluding:
sums = excluding
result.append(num)
else:
sums = including
result.append(-1 * num)
return result