Construct Target Array With Multiple Sums Problem
Description
LeetCode Problem 1354.
You are given an array target of n integers. From a starting array arr consisting of n 1's, you may perform the following procedure :
- let
xbe the sum of all elements currently in your array. - choose index
i, such that0 <= i < nand set the value ofarrat indexitox. - You may repeat this procedure as many times as needed.
Return true if it is possible to construct the target array from arr, otherwise, return false.
Example 1:
Input: target = [9,3,5]
Output: true
Explanation: Start with arr = [1, 1, 1]
[1, 1, 1], sum = 3 choose index 1
[1, 3, 1], sum = 5 choose index 2
[1, 3, 5], sum = 9 choose index 0
[9, 3, 5] Done
Example 2:
Input: target = [1,1,1,2]
Output: false
Explanation: Impossible to create target array from [1,1,1,1].
Example 3:
Input: target = [8,5]
Output: true
Constraints:
n == target.length1 <= n <= 5 * 10^41 <= target[i] <= 10^9
Solution
Approach: Work Backwards with a Max-Heap
Going forwards is hard: at every step we could overwrite any index. Going backwards is easy, because the step that produced the current array is forced. The value just written equals the sum of the whole array before it, and every element is at least 1, so that value is strictly larger than everything else. In other words, the last element written is always the current maximum.
Let mx be the maximum and rest the sum of the other elements. Before the last step, the array summed to mx, and the other elements were already rest. So the old value at that index was mx - rest. Repeat until every element is 1:
- If
rest == 0(only one element) ormx <= rest, the old value would be below 1, so the target cannot be built: returnfalse. - Otherwise the old value is
mx - rest.
Subtracting one rest at a time is too slow: [1, 1000000000] would take a billion steps, since the 1 keeps the maximum in the same spot. While the same element stays the maximum, we keep subtracting the same rest, so we can jump straight to mx % rest. If that is 0, the element would drop to rest or below and fail on the next check, except when rest == 1, where it simply ends at 1. Using mx % rest, or rest when the remainder is 0, covers both.
Keep the elements in a max-heap and the running total in a 64-bit integer (it can reach about 5 * 1013). Pop the maximum, replace it by its old value, push it back, and stop when the top is 1.
Complexity: O(n log n) time. Filling the heap costs O(n log n); after that, each undo shrinks the total by at least a third (the new total is rest + mx % rest, which is at most mx and below 2 * rest), so there are only O(log S) undo steps of O(log n) each, where S is the sum of target. O(n) space for the heap.
import heapq
class Solution:
def isPossible(self, target: List[int]) -> bool:
total = sum(target)
heap = [-x for x in target] # max-heap through negated values
heapq.heapify(heap)
while -heap[0] > 1:
mx = -heapq.heappop(heap)
rest = total - mx # sum of all the other elements
if rest == 0 or mx <= rest:
return False # n == 1, or mx could not have been the last sum
prev = mx % rest or rest # undo many steps at once; rest == 1 lands on 1
heapq.heappush(heap, -prev)
total = rest + prev
return True