Diet Plan Performance in Python

A diet plan performance problem tracks a dieter's points based on their calorie consumption over consecutive k days. The dieter gains or loses points depending on whether their total calories fall below a lower bound, above an upper bound, or within the normal range.

Problem Description

Given an array calories[i] representing daily calorie consumption, we need to evaluate every consecutive sequence of k days and calculate points based on these rules:

  • If total calories < lower bound: lose 1 point
  • If total calories > upper bound: gain 1 point
  • Otherwise: no change in points

The dieter starts with zero points, and we need to find the final point total.

Example Walkthrough

For array [6,5,0,0] with k=2, lower=1, and upper=5:

  • Days 0−1: 6+5=11 > 5 ? gain 1 point (total: 1)
  • Days 1−2: 5+0=5 ? 5 ? no change (total: 1)
  • Days 2−3: 0+0=0 ? lose 1 point (total: 0)

Solution Using Sliding Window

We use a sliding window approach to efficiently calculate consecutive sums without recalculating from scratch each time:

class Solution:
    def dietPlanPerformance(self, calories, k, lower, upper):
        # Calculate sum of first k days
        window_sum = sum(calories[:k])
        points = 0
        
        # Evaluate all k-day windows
        for i in range(len(calories) - k + 1):
            # Update window sum for current position
            if i > 0:
                window_sum = window_sum - calories[i-1] + calories[i+k-1]
            
            # Update points based on current window sum
            if window_sum < lower:
                points -= 1
            elif window_sum > upper:
                points += 1
        
        return points

# Test the solution
solution = Solution()
result = solution.dietPlanPerformance([6, 5, 0, 0], 2, 1, 5)
print("Points earned:", result)
Points earned: 0

Alternative Implementation

Here's the original approach using explicit left and right pointers:

def diet_plan_performance(calories, k, lower, upper):
    # Initialize first window sum
    temp = sum(calories[:k])
    left = 0
    points = 0
    
    # Process each k-day window
    for right in range(k-1, len(calories)):
        # Check current window and update points
        if temp < lower:
            points -= 1
        elif temp > upper:
            points += 1
        
        # Slide the window (except for last iteration)
        if right < len(calories) - 1:
            temp = temp - calories[left] + calories[right + 1]
            left += 1
    
    return points

# Test the function
result = diet_plan_performance([6, 5, 0, 0], 2, 1, 5)
print("Final points:", result)

# Test with another example
result2 = diet_plan_performance([1, 2, 3, 4, 5], 3, 3, 10)
print("Points for [1,2,3,4,5] with k=3:", result2)
Final points: 0
Points for [1,2,3,4,5] with k=3: 1

Key Points

  • Time Complexity: O(n) where n is the length of calories array
  • Space Complexity: O(1) as we only use constant extra space
  • Sliding Window: Efficiently updates sum by removing the leftmost element and adding the new rightmost element
  • Boundary Conditions: Handles arrays where length equals k correctly

Conclusion

The diet plan performance problem is efficiently solved using a sliding window technique. By maintaining a running sum and updating it incrementally, we can evaluate all consecutive k−day periods in linear time while tracking the dieter's point changes based on their calorie consumption patterns.

Updated on: 2026-03-25T08:09:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements