Diet Plan Performance
Problem Statement
A dieter consumes calories[i]
calories on the i-th
day.
Given an integer k
, the dieter reviews their calorie intake over every sequence of k
consecutive days (from calories[i]
to calories[i + k - 1]
for all 0 <= i <= n - k
). For each sequence, they calculate T
, the total calories consumed over those k
days:
- If
T
is less thanlower
, the dieter performs poorly and loses 1 point. - If
T
is greater thanupper
, the dieter performs better and gains 1 point. - If
T
is betweenlower
andupper
(inclusive), the dieter’s performance is normal, and their points remain the same.
The dieter starts with zero points. Return the total points after the dieter follows this routine for all calories.length
days. The total points can be negative.
Constraints
- 1 ≤ k ≤
calories.length
≤ 10^5 - 0 ≤
calories[i]
≤ 20000 - 0 ≤
lower
≤ upper
Examples
Example 1:
Input:
calories: [2, 3, 5, 7, 8]
k = 1
lower = 5
upper = 5
Output:
0
Explanation:
k = 1, so we consider each element of the array separately.
calories[0] < lower, so -1 point.
calories[1] < lower, so -1 point.
calories[2] == lower, so no point.
calories[3] > upper, so 1 point.
calories[4] > upper, so 1 point.
Example 2:
Input:
calories: [2, 3, 5]
k = 2
lower = 4
upper = 6
Output:
1
Explanation:
calories[0] + calories[1] is in between lower and upper, so no point.
calories[1] + calories[2] > upper, so 1 point.
Example 3:
Input:
calories: [0, 0, 0]
k = 3
lower = 2
upper = 4
Output:
-1
Explanation:
calories[0] + calories[1] + calories[2] < lower, so -1 point.
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Test yourself!
If a dieter consumes 4 calories each day for 5 days with k = 3, lower = 10, and upper = 12, what are their final points?
Test yourself!
Which scenario would result in the dieter gaining points?
Test yourself!
If the dieter’s initial points are 0 and they consume calories [3, 5, 8, 2, 6] over 5 days with k = 2, lower = 7, and upper = 10, what are the final points?