1354. Construct Target Array With Multiple Sums (Hard)
构建目标数组问题。给你一个长度为 n 的目标数组 target,你需要从一个包含 n 个 1 的数组 arr 开始执行下面的过程:
- 设当前的
arr数组的和为x; - 选择
0到n之间的一个下标i,让arr[i]等于x; - 重复这个过程。
判断最终能否构建出目标数组。
Highlight:
- Backward from the max value
- Mod operation on max value with rest sum to get replacement value
- Push replacement value back to heap
- Repeat the procedure until the max value becomes to 1, which means succeed
- Or until the exception occurs and return
False- when the rest sum is greater than the max value
- when the rest sum less than 1
class Solution: |
相关文章