LeetCode #1176. Diet Plan Performance
题目
解题方法
设置points=0,T初始化为sum(calories[0:k]),从数组中第k-1位开始遍历数组,先判断积分情况,然后在数组上做一个长度为k的滑动窗口,更新T的值即可。
时间复杂度:O(n)
空间复杂度:O(1)
代码
class Solution:
def dietPlanPerformance(self, calories: List[int], k: int, lower: int, upper: int) -> int:
points = 0
T = sum(calories[0:k])
for i in range(k-1, len(calories)):
if T < lower: points -= 1
elif T > upper: points += 1
else: pass
if i+1 != len(calories):
T = T - calories[i-k+1] + calories[i+1]
return points