Construct Target Array With Multiple Sums LeetCode solution in Python

1
# Runtime: 492 ms (Top 22.46%) | Memory: 20 MB (Top 17.02%)
2
class Solution:
3
def isPossible(self, target: List[int]) -> bool:
4
if len(target) == 1:
5
return target == [1]
6
res = sum(target)
7
heap = [-elem for elem in target]
8
heapify(heap)
9
while heap[0] < -1:
10
maximum = -heappop(heap)
11
res -= maximum
12

13
if res == 1:
14
return True
15
x = maximum % res
16
if x == 0 or (x != 1 and x == maximum):
17
return False
18

19
res += x
20
heappush(heap, -x)
21
return True

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0