思路
对于每种隐藏数组,一旦确定开头,后续数组序列都是确定的,问题转化为lower到upper间的每种情况中,有多少是满足条件的;
而条件的限制是全在范围内,因此累加diff数组,记录最低会减多少,最高会加多少,如果开头加上这个最大变化幅度在范围内就是可行答案;
会爆int,所以要用long long
代码
class Solution {
public:int numberOfArrays(vector<int>& diff, int lower, int upper) {long long small &#61; 1e6, big &#61; -1e6;int n &#61; diff.size();long long start &#61; 0;for(int i &#61; 0; i < n; i&#43;&#43;) {start &#43;&#61; diff[i];small &#61; min(start, small);big &#61; max(start, big);}int ans &#61; 0;for(long long i &#61; lower; i <&#61; upper; i&#43;&#43;) {if(i &#43; small >&#61; lower && i &#43; big <&#61; upper) ans&#43;&#43;;}return ans;}
};