Diet Plan Performance - Problem
Diet Plan Performance Tracker

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
๐Ÿ‹๏ธ Diet Plan Performance TrackerDaily Calories:12345Window 1 (k=3)Window EvaluationSum = 1 + 2 + 3 = 6Lower = 8, Upper = 106 < 8 โ†’ Poor performanceScore: -1 point ๐Ÿ˜žSliding to Next Window:2345Window 2 (k=3)Remove: -1Add: +4New Sum = 6 - 1 + 4 = 9โšก Sliding Window Benefitsโœ“ O(n) time instead of O(nร—k)โœ“ No redundant sum calculationsโœ“ Constant O(1) space usageโœ“ Efficient sliding techniquePerfect for subarray problems!๐ŸŽฏ Result: Optimal O(n) solution with sliding window technique
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.
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen