六一儿童节的糖
这道题用到的是前缀和,只要保证我们能吃的糖果数量的最大值和最小值的区间,和要吃到目标糖果所需吃的糖果数量的最大值和最小值的区间,这两个区间有交集,我们就能吃目标天数吃到我们想吃的糖果。
class Solution:
def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
#首先计算前缀和
total = list(accumulate(candiesCount))
answer = list()
for favoriteType, favoriteDay, dailyCap in queries:
mineatsugar = favoriteDay + 1
maxeatsugar = (favoriteDay + 1) * dailyCap
mintargetsugar = 1 if favoriteType == 0 else total[favoriteType - 1] + 1
maxtargetsugar = total[favoriteType]
answer.append(not(mineatsugar > maxtargetsugar or mintargetsugar > maxeatsugar))
return answer