536. Diet Plan Performance
A dieter tracks their calorie intake over several days, represented as an array calories[i].
Given an integer k, for each consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they calculate T, the total calories consumed in that sequence:
- If T < lower, they performed poorly and lose 1 point.
- If T > upper, they performed well and gain 1 point.
- Otherwise, they performed normally, and no points are gained or lost.
Initially, the dieter has zero points. Return the total number of points the dieter has after calories.length days.
The total points can be negative.
Example 1:
Input: calories = [2, 4, 6, 8, 10], k = 2, lower = 7, upper = 9
Output: 2
Explanation:
We check all consecutive subarrays of length k = 2:
- calories[0] + calories[1] = 2 + 4 = 6 (less than lower → lose 1 point)
- calories[1] + calories[2] = 4 + 6 = 10 (greater than upper → gain 1 point)
- calories[2] + calories[3] = 6 + 8 = 14 (greater than upper → gain 1 point)
calories[3] + calories[4] = 8 + 10 = 18 (greater than upper → gain 1 point)
- Total points: -1 + 1 + 1 + 1 = 2.
Example 2:
Input: calories = [1, 1, 1, 1, 1], k = 3, lower = 4, upper = 5
Output: -3
Explanation:
We check all consecutive subarrays of length k = 3:
- 1 + 1 + 1 = 3 (less than lower → lose 1 point)
- 1 + 1 + 1 = 3 (less than lower → lose 1 point)
- 1 + 1 + 1 = 3 (less than lower → lose 1 point)
- Total points: -1 -1 -1 = -3.
Now Your Turn!
Pick the correct output for the given inputInput: calories = [5, 10, 15, 20], k = 2, lower = 12, upper = 18
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= k <= calories.length <= 105
- 0 <= calories[i] <= 20000
- 0 <= lower <= upper