{x}
blog image

Diet Plan Performance

Solution Explanation for LeetCode 1176. Diet Plan Performance

This problem involves calculating a dieter's performance score based on their daily calorie intake over a given period. The solution can be efficiently implemented using two main approaches: Prefix Sum and Sliding Window. Both achieve a linear time complexity, but the Sliding Window approach offers a slightly smaller space complexity.

Problem Description:

The input consists of an array calories representing daily calorie intake, an integer k specifying the window size (number of consecutive days to consider), and integers lower and upper defining the calorie thresholds. The dieter's score increases by 1 if the total calories within a k-day window exceed upper, decreases by 1 if it's below lower, and remains unchanged otherwise. The goal is to compute the final score.

Approach 1: Prefix Sum

  1. Prefix Sum Array: Create a prefix sum array s. s[i] stores the cumulative sum of calories from day 0 to day i-1. This allows for efficient calculation of the sum within any window.

  2. Window Sum Calculation: Iterate through the prefix sum array. For each index i, the sum of calories for the k-day window starting at day i is given by s[i + k] - s[i].

  3. Score Update: Compare the window sum with lower and upper and adjust the score accordingly.

Time Complexity: O(n), where n is the length of the calories array. We iterate through the array once to create the prefix sum array and then again to calculate window sums.

Space Complexity: O(n) due to the prefix sum array.

Approach 2: Sliding Window

  1. Initial Window Sum: Calculate the sum of the first k elements in calories. This represents the initial sliding window.

  2. Sliding the Window: Iterate through the remaining elements of calories. In each iteration:

    • Subtract the element leaving the window from the current sum.
    • Add the new element entering the window to the current sum.
    • Update the score based on the updated window sum.

Time Complexity: O(n). We iterate through the array once.

Space Complexity: O(1). We only use a few constant extra variables to store the sum and score.

Code Examples (Python):

Approach 1: Prefix Sum

from itertools import accumulate
 
def dietPlanPerformance(calories, k, lower, upper):
    s = list(accumulate(calories, initial=0))
    ans = 0
    for i in range(len(calories) - k + 1):
        window_sum = s[i + k] - s[i]
        if window_sum < lower:
            ans -= 1
        elif window_sum > upper:
            ans += 1
    return ans
 

Approach 2: Sliding Window

def dietPlanPerformance(calories, k, lower, upper):
    window_sum = sum(calories[:k])
    ans = 0
    if window_sum < lower:
        ans -= 1
    elif window_sum > upper:
        ans += 1
    for i in range(k, len(calories)):
        window_sum += calories[i] - calories[i - k]
        if window_sum < lower:
            ans -= 1
        elif window_sum > upper:
            ans += 1
    return ans

Both approaches provide correct solutions. The Sliding Window approach is generally preferred for its slightly better space efficiency, especially when dealing with very large input arrays. The code examples in other languages (Java, C++, Go, TypeScript) would follow similar logic and structure to these Python examples.