Problem
Given an array of integers target. From a starting array, A consisting of all 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 < target.sizeand set the value ofAat indexitox. - You may repeat this procedure as many times as needed.
Return True if it is possible to construct the target array from A otherwise return False.
Example 1:
1 | Input: target = [9,3,5] |
Example 2:
1 | Input: target = [1,1,1,2] |
Example 3:
1 | Input: target = [8,5] |
Constraints:
N == target.length1 <= target.length <= 5 * 10^41 <= target[i] <= 10^9
Analysis
题目给出一个target数组,从一开始有的是全是1的数组,每次操作可以把这个数组的和,替换掉数组中的某个值。问这个target数组能否按照这种规则生成出来。我们先看Example1,题目中有解释答案是怎么计算出来的。我们不妨反向看是怎么从结果倒推回去初始状态的。我们先计算出一个sum = 17,然后我们找到数组中最大的数,然后用sum减去这个数,同时把这个数移除出数组。移除之后,我们需要把原来的数放回去数组,完成替换。这里特别注意,计算原来的数的时候,不能直接用减法,会TLE,要用取余操作替代。
中间的转化分析完,然后就要看终止条件。有几个终止条件:
- 当sum = 1时,说明数组中有两个数,除了最大值之外,剩下的就是1,如
[1, 3],这种情况下是一定能成功的; - 当最大值为1,说明数组中只有1存在,这是绝大多数变换到最后的情况,所以也是成功的;
- 当sum为0,说明数组中只有一个元素且不是1,因此转换失败;
- 当sum比top大,说明需要替换的元素是个负数,不符合题意,转换失败;
- 当
top % sum == 0时,说明替换的元素是0,也不符合题意,转换失败。
Solution
维护数组中最大值很明显就是用大堆了。还需要注意这里sum要使用long类型,然后在使用accumulate的时候,初始值要用0LL,而不是0。
Code
1 | class Solution { |
Summary
这道题目是难度比较大的数组题目,其算法思路较为复杂,同时还需要考虑到多种边界case。这道题目的分享到这里,感谢你的支持!