Suppose a dieter consumes calories[i], this indicates the calories on the i-th day. If we have an integer k, for every consecutive sequence of k days that is (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they find T. The T is the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]): But there are some conditions;
At first, the dieter has zero points. We have to find the total number of points the dieter has gained.
If the array is [6,5,0,0] and k = 2 and lower := 1 and upper := 5, then the output will be 0. C + C > upper, so increase one point, lower <= C + C <= upper so no change, after that C + C < lower, so decrease one point, so the output will be 0.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution(object): def dietPlanPerformance(self, c, k, l, u): temp = 0 for i in range(k): temp += c[i] right = k-1 left = 0 points = 0 while right < len(c): if temp<l: points-=1 elif temp>u: points+=1 temp -=c[left] left+=1 right+=1 if(right >= len(c)): break temp+=c[right] return points ob1 = Solution() print(ob1.dietPlanPerformance([6,5,0,0],2,1,5))
[6,5,0,0] 2 1 5