File tree 1 file changed +8
-7
lines changed
scripts/algorithms/F/Find Array Given Subset Sums
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 4011 ms (Top 18.39%) | Memory: 19.7 MB (Top 40.23%)
1
2
class Solution :
2
3
def recoverArray (self , n : int , sums : List [int ]) -> List [int ]:
3
- res = [] # Result set
4
+ res = [] # Result set
4
5
sums .sort ()
5
-
6
+
6
7
while len (sums ) > 1 :
7
8
num = sums [- 1 ] - sums [- 2 ] # max - secondMax
8
9
countMap = Counter (sums ) # Get count of each elements
9
10
excluding = [] # Subset sums that do NOT contain num
10
11
including = [] # Subset sums that contain num
11
-
12
+
12
13
for x in sums :
13
14
if countMap .get (x ) > 0 :
14
15
excluding .append (x )
15
16
including .append (x + num )
16
17
countMap [x ] -= 1
17
18
countMap [x + num ] -= 1
18
-
19
- # Check validity of excluding set
19
+
20
+ # Check validity of excluding set
20
21
if 0 in excluding :
21
22
sums = excluding
22
23
res .append (num )
23
24
else :
24
25
sums = including
25
26
res .append (- 1 * num )
26
-
27
- return res
27
+
28
+ return res
You can’t perform that action at this time.
0 commit comments