Find Array Given Subset Sums LeetCode solution in Python
1# Runtime: 4011 ms (Top 18.39%) | Memory: 19.7 MB (Top 40.23%)2class Solution:3def recoverArray(self, n: int, sums: List[int]) -> List[int]:4res = [] # Result set5sums.sort()67while len(sums) > 1:8num = sums[-1] - sums[-2] # max - secondMax9countMap = Counter(sums) # Get count of each elements10excluding = [] # Subset sums that do NOT contain num11including = [] # Subset sums that contain num1213for x in sums:14if countMap.get(x) > 0:15excluding.append(x)16including.append(x + num)17countMap[x] -= 118countMap[x + num] -= 11920# Check validity of excluding set21if 0 in excluding:22sums = excluding23res.append(num)24else:25sums = including26res.append(-1 * num)2728return res