Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1176.html
1176. Diet Plan Performance
Level
Easy
Description
A dieter consumes calories[i]
calories on the i
th day.
Given an integer k
, for every consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k1]
for all 0 <= i <= nk
), they look at T, the total calories consumed during that sequence of k
days (calories[i] + calories[i+1] + ... + calories[i+k1]
):
 If
T < lower
, they performed poorly on their diet and lose 1 point;  If
T > upper
, they performed well on their diet and gain 1 point;  Otherwise, they performed normally and there is no change in points.
Initially, the dieter has zero points. Return the total number of points the dieter has after dieting for calories.length
days.
Note that the total points can be negative.
Example 1:
Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
Output: 0
Explanation: Since k = 1, we consider each element of the array separately and compare it to lower and upper.
calories[0] and calories[1] are less than lower so 2 points are lost.
calories[3] and calories[4] are greater than upper so 2 points are gained.
Example 2:
Input: calories = [3,2], k = 2, lower = 0, upper = 1
Output: 1
Explanation: Since k = 2, we consider subarrays of length 2.
calories[0] + calories[1] > upper so 1 point is gained.
Example 3:
Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5
Output: 0
Explanation:
calories[0] + calories[1] > upper so 1 point is gained.
lower <= calories[1] + calories[2] <= upper so no change in points. calories[2] + calories[3] < lower so 1 point is lost.
Constraints:
1 <= k <= calories.length <= 10^5
0 <= calories[i] <= 20000
0 <= lower <= upper
Solution
Use the idea of sliding window. Initially, calculate the calories consumed during the first consecutive k
days, which is calories[0] + calories[1] + ... + calories[k  1]
. Let sum
be the the calories consumed during the first consecutive k
days. If sum < lower
, then lose 1 point. If sum > upper
, then gain 1 point. Each time, remove the first element from the window and add the next element into the window and calculate the sum, and decided whether the point is increased, decreased or unchanged.
Finally, return the points.

class Solution { public int dietPlanPerformance(int[] calories, int k, int lower, int upper) { int points = 0; int sum = 0; for (int i = 0; i < k; i++) sum += calories[i]; if (sum > upper) points++; else if (sum < lower) points; int length = calories.length; for (int i = k; i < length; i++) { sum += calories[i]; sum = calories[i  k]; if (sum > upper) points++; else if (sum < lower) points; } return points; } }

// OJ: https://leetcode.com/problems/dietplanperformance/ // Time: O(N) // Space: O(1) class Solution { public: int dietPlanPerformance(vector<int>& A, int k, int lower, int upper) { int N = A.size(), ans = 0, sum = 0; for (int i = 0; i < N; ++i) { sum += A[i]; if (i  k >= 0) sum = A[i  k]; if (i >= k  1) { if (sum < lower) ans; else if (sum > upper) ++ans; } } return ans; } };

class Solution: def dietPlanPerformance( self, calories: List[int], k: int, lower: int, upper: int ) > int: s = list(accumulate(calories, initial=0)) ans, n = 0, len(calories) for i in range(n  k + 1): t = s[i + k]  s[i] if t < lower: ans = 1 elif t > upper: ans += 1 return ans

func dietPlanPerformance(calories []int, k int, lower int, upper int) int { n := len(calories) s := make([]int, n+1) for i, v := range calories { s[i+1] = s[i] + v } ans := 0 for i := 0; i < nk+1; i++ { t := s[i+k]  s[i] if t < lower { ans } else if t > upper { ans++ } } return ans }