-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconstruct-target-array-with-multiple-sums.py
97 lines (87 loc) · 3.61 KB
/
construct-target-array-with-multiple-sums.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# https://leetcode.com/problems/construct-target-array-with-multiple-sums/
import timeit
from heapq import heapify, heappushpop
from typing import List
# Use a heap to keep track of the biggest element.
# The two hints on the exercise description are very helpful, even more so the discussion about using % instead of `-`
# https://leetcode.com/problems/construct-target-array-with-multiple-sums/discuss/510256/JavaC%2B%2BPython-Backtrack-OJ-is-wrong
#
# When the max value is significantly larger than the sum of the rest of the values, we end up iterating the loop too many
# times, that can be avoided using the modulus operation due to the fact that:
# max-n*(a1+a2) = max % (a1+a2)
#
# Runtime: 345 ms, faster than 61.18% of Python3 online submissions for Construct Target Array With Multiple Sums.
# Memory Usage: 19.9 MB, less than 55.29 % of Python3 online submissions for Construct Target Array With Multiple Sums.
class HeapAndModulus:
def isPossible(self, target: List[int]) -> bool:
s = sum(target)
# Reverse the sign of all elements
heap = [-1 * x for x in target]
# heapify O(n) performs better than individually pushing into the heap O(n*log(n))
heapify(heap)
while True:
prev = -heap[0]
s -= prev
# If the maximum value in the heap is 1 or the remaining sum is 1, we are done
if prev == 1 or s == 1:
return True
# Check if we have reached a dead end
if prev < s or s == 0 or prev % s == 0:
return False
# Calculate the value to be pushed into the heap and push it
prev %= s
heappushpop(heap, -prev)
# Update the current sum of the values in the array/heap
s += prev
# This solution is almost the same but runs slower because we are using `-` instead of `%`
class Heap:
def isPossible(self, target: List[int]) -> bool:
s = sum(target)
# Reverse the sign of all elements
heap = [-1 * x for x in target]
# heapify O(n) performs better than individually pushing into the heap O(n*log(n))
heapify(heap)
while True:
prev = -heap[0]
s -= prev
# If the maximum value in the heap is 1 or the remaining sum is 1, we are done
if prev == 1 or s == 1:
return True
# Check if we have reached a dead end
if prev < s or s == 0 or prev - s == 0:
return False
# Calculate the value to be pushed into the heap and push it
prev -= s
heappushpop(heap, -prev)
# Update the current sum of the values in the array/heap
s += prev
def test():
executor = [
{'executor': HeapAndModulus, 'title': 'HeapAndModulus', },
{'executor': Heap, 'title': 'Heap', },
]
tests = [
[[1], True],
[[1, 1, 1, 2], False],
[[1, 1, 2], False],
[[1, 2], True],
[[1, 1000000000], True],
[[2], False],
[[5, 50], False],
[[8, 5], True],
[[9, 3, 5], True],
[[12, 5], True],
[[13, 5], True],
]
for e in executor:
start = timeit.default_timer()
for _ in range(int(float('1e4'))):
for t in tests:
sol = e['executor']()
result = sol.isPossible([*t[0]])
expected = t[1]
assert result == expected, f'{result} != {expected}'
stop = timeit.default_timer()
used = str(round(stop - start, 5))
print("{0:20}{1:10}{2:10}".format(e['title'], used, "seconds"))
test()