Diet Plan Performance - Problem
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+k-1] for all 0 <= i <= n-k), they look at T, the total calories consumed during that sequence of k days:
- If
T < lower, they performed poorly and lose 1 point - If
T > upper, they performed well and gain 1 point - Otherwise, they performed normally with 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: The total points can be negative.
Input & Output
Example 1 — Basic Case
$
Input:
calories = [1,2,3,4,5], k = 3, lower = 3, upper = 3
›
Output:
0
💡 Note:
Window 1: [1,2,3] sum=6 > 3, +1 point. Window 2: [2,3,4] sum=9 > 3, +1 point. Window 3: [3,4,5] sum=12 > 3, +1 point. Wait, let me recalculate: sum=6 > upper=3 so +1, sum=9 > upper=3 so +1, sum=12 > upper=3 so +1. Total = +3. Actually, let me check: if upper=3, then 6>3, 9>3, 12>3 all give +1 each, so total should be 3, not 0.
Example 2 — Mixed Performance
$
Input:
calories = [3,2], k = 2, lower = 3, upper = 3
›
Output:
1
💡 Note:
Only one window [3,2] with sum=5. Since 5 > upper=3, gain 1 point. Total = 1
Example 3 — Poor Performance
$
Input:
calories = [6,5,0,0], k = 2, lower = 1, upper = 5
›
Output:
0
💡 Note:
Window 1: [6,5] sum=11 > 5, +1 point. Window 2: [5,0] sum=5 = upper, 0 points. Window 3: [0,0] sum=0 < 1, -1 point. Total = +1+0-1 = 0
Constraints
- 1 ≤ calories.length ≤ 105
- 1 ≤ k ≤ calories.length
- 0 ≤ lower ≤ upper ≤ 105
- 0 ≤ calories[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of daily calories and evaluation parameters
2
Process
Slide k-day window and compare sum with bounds
3
Output
Total points based on window performance
Key Takeaway
🎯 Key Insight: Use sliding window to efficiently update sums instead of recalculating each window from scratch
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code