题目链接
前缀和思想,
v
[
i
]
v[i]
v[i] 表示前
i
−
1
i-1
i−1 天糖果数量总合,判断一下每天一颗糖果是否在第
f
a
v
o
r
i
t
e
D
a
y
i
favoriteDay_i
favoriteDayi 吃到糖果,并且最多能吃的糖果数是否大于
f
a
v
o
r
i
t
e
D
a
y
i
favoriteDay_i
favoriteDayi 前吃的糖果数量总合。
class Solution {
public:
typedef long long ll;
vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
int n = candiesCount.size();
vector<ll> v(n+1);
ll res = 0;
for(int i = 0; i < n; i++)
{
v[i] = res;
res += candiesCount[i];
}
v[n] = res;
vector<bool> ans;
for(auto& x : queries)
{
int id = x[0]; ll day = x[1], eve = x[2];
ll t = eve*(day+1);
if(day < v[id+1] && t > v[id]) ans.push_back(true);
else ans.push_back(false);
}
return ans;
}
};