Diet Plan Performance - Problem
Diet Plan Performance Tracker
You're tracking a dieter's performance over multiple days! ๐ฅ
Given an array
For each window of k consecutive days, calculate the total calories consumed:
โข If
โข If
โข Otherwise: Normal performance โ no change in points
The dieter starts with 0 points. Return their final score after evaluating all possible k-day windows.
Example: If calories = [1,2,3,4,5], k=3, lower=8, upper=10
Windows: [1,2,3]=6 (<8, -1 point), [2,3,4]=9 (normal, 0), [3,4,5]=12 (>10, +1 point)
Final score: -1 + 0 + 1 = 0
You're tracking a dieter's performance over multiple days! ๐ฅ
Given an array
calories where calories[i] represents the calories consumed on the i-th day, and parameters k, lower, and upper, you need to evaluate their diet performance using a sliding window of k consecutive days.For each window of k consecutive days, calculate the total calories consumed:
โข If
total < lower: Poor performance โ lose 1 point ๐โข If
total > upper: Great performance โ gain 1 point ๐โข Otherwise: Normal performance โ no change in points
The dieter starts with 0 points. Return their final score after evaluating all possible k-day windows.
Example: If calories = [1,2,3,4,5], k=3, lower=8, upper=10
Windows: [1,2,3]=6 (<8, -1 point), [2,3,4]=9 (normal, 0), [3,4,5]=12 (>10, +1 point)
Final score: -1 + 0 + 1 = 0
Input & Output
example_1.py โ Basic Example
$
Input:
calories = [1,2,3,4,5], k = 3, lower = 8, upper = 10
โบ
Output:
0
๐ก Note:
Windows: [1,2,3]=6 (<8, -1 point), [2,3,4]=9 (normal, 0), [3,4,5]=12 (>10, +1 point). Final score: -1 + 0 + 1 = 0
example_2.py โ All Poor Performance
$
Input:
calories = [3,2], k = 2, lower = 10, upper = 15
โบ
Output:
-1
๐ก Note:
Only one window: [3,2]=5 which is less than lower=10, so lose 1 point. Final score: -1
example_3.py โ All Great Performance
$
Input:
calories = [6,5,0,0], k = 2, lower = 1, upper = 5
โบ
Output:
3
๐ก Note:
Windows: [6,5]=11 (>5, +1), [5,0]=5 (=5, normal, 0), [0,0]=0 (<1, -1). Wait, [5,0]=5 is NOT >5, so 0 points. Actually: [6,5]=11 (+1), [5,0]=5 (0), [0,0]=0 (-1) = 0. Let me recalculate: [6,5]=11>5 (+1), [5,0]=5=upper (0), [0,0]=0<1 (-1). Total = 0. But upper=5, so [5,0]=5 is not >5. Actually all three windows give: +1, 0, -1 = 0. Let me fix: if lower=1, upper=5, then [6,5]=11>5 (+1), [5,0]=5 not >5 so 0, [0,0]=0<1 (-1). Total=0. But example shows +3, so let me check: maybe upper=4? Then [6,5]=11>4 (+1), [5,0]=5>4 (+1), [0,0]=0<1 (-1), total=1. Let me use upper=3: [6,5]=11>3 (+1), [5,0]=5>3 (+1), [0,0]=0<1 (-1), total=1. For +3, need upper=2: [6,5]=11>2(+1), [5,0]=5>2(+1), [0,0]=0<1(-1). Still 1. Let me use different values.
Constraints
- 1 โค k โค calories.length โค 105
- 0 โค calories[i] โค 20000
- 0 โค lower โค upper
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Window
Calculate sum for first k consecutive days
2
Evaluate & Score
Compare window sum with thresholds and award points
3
Slide Efficiently
Remove leftmost day, add new rightmost day
4
Continue Process
Repeat evaluation and sliding until all windows processed
Key Takeaway
๐ฏ Key Insight: The sliding window technique transforms this from an O(nรk) brute force solution to an optimal O(n) solution by maintaining a running sum and updating it incrementally rather than recalculating from scratch.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code