A dieter consumes calories[i] calories on the ith day.
Given an integer array calories representing the number of calories consumed each day, and an integer k representing the rolling average days, and two integers lower and upper representing the acceptable range of average calories, you need to calculate the diet plan performance.
Your diet plan performance is calculated as follows:
k consecutive days is strictly less than lower, award 1 point.k consecutive days is strictly greater than upper, award 1 point.0 points.Return the total number of points you get.
Note that the average is calculated as the sum of the k consecutive days divided by k, using integer division.
Example 1:
Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3 Output: 0 Explanation: Calories Consumed in each day day 1 (1) < lower (3) => 1 point day 2 (2) < lower (3) => 1 point day 3 (3) == lower (3) => 0 points day 4 (4) > upper (3) => 1 point day 5 (5) > upper (3) => 1 point Total points = 1 + 1 + 0 + 1 + 1 = 4
Example 2:
Input: calories = [3,2,1], k = 2, lower = 2, upper = 3 Output: 1 Explanation: The calories consumed for each consecutive 2 days are (3+2)/2 = 2, (2+1)/2 = 1. The first day (2) == lower (2) => 0 points The second day (1) < lower (2) => 1 point Total points = 0 + 1 = 1
Example 3:
Input: calories = [6,5,4,3,2,1], k = 2, lower = 2, upper = 3 Output: 0 Explanation: The calories consumed for each consecutive 2 days are (6+5)/2 = 5, (5+4)/2 = 4, (4+3)/2 = 3, (3+2)/2 = 2, (2+1)/2 = 1. The first day (5) > upper (3) => 1 point The second day (4) > upper (3) => 1 point The third day (3) == upper (3) => 0 points The fourth day (2) == lower (2) => 0 points The fifth day (1) < lower (2) => 1 point Total points = 1 + 1 + 0 + 0 + 1 = 3
Constraints:
1 <= k <= calories.length <= 1050 <= calories[i] <= 200000 <= lower <= upperWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this diet problem means we're going to try every single possible combination of days to see if they meet the weight loss/gain criteria. We will examine every possible consecutive sequence of days within the provided data.
Here's how the algorithm would work step-by-step:
def diet_plan_performance_brute_force(calories, lower_limit, upper_limit):
poor_performance_days = 0
for start_day_index in range(len(calories)):
current_calories_sum = 0
for end_day_index in range(start_day_index, len(calories)):
current_calories_sum += calories[end_day_index]
# Accumulate the total to check against thresholds
if current_calories_sum < lower_limit or current_calories_sum > upper_limit:
poor_performance_days += 1
return poor_performance_daysWe need to efficiently figure out how well someone is sticking to their diet. The key is to look at chunks of days at a time to see if they exceeded the limit or fell short. This allows us to calculate the performance score without checking every single day individually.
Here's how the algorithm would work step-by-step:
def diet_plan_performance(calories, day_window, lower_limit, upper_limit):
performance_score = 0
current_calories_sum = sum(calories[:day_window])
# Check performance for initial window
if current_calories_sum < lower_limit:
performance_score -= 1
elif current_calories_sum > upper_limit:
performance_score += 1
# Iterate through remaining windows
for i in range(day_window, len(calories)):
# Slide the window by subtracting the oldest day and adding the newest day.
current_calories_sum += calories[i] - calories[i - day_window]
# Update performance score based on calorie consumption.
if current_calories_sum < lower_limit:
performance_score -= 1
elif current_calories_sum > upper_limit:
performance_score += 1
return performance_score| Case | How to Handle |
|---|---|
| Empty calories array | Return 0 for all windows since no calories can be summed. |
| Calories array size smaller than k | Return 0 since no window of size k exists. |
| k is zero or negative | Treat k as invalid and return 0 as no meaningful window exists. |
| Calories array contains negative values | The problem statement is valid with negative numbers, so the sliding window approach should correctly account for weight loss and gains. |
| Calories array contains very large values leading to integer overflow when summed | Use a larger integer data type like long to store the sum of calories. |
| All windows have calories sum greater than upper threshold | The solution will consistently decrement the performance score for all windows. |
| All windows have calories sum less than lower threshold | The solution will consistently increment the performance score for all windows. |
| Upper threshold equals lower threshold | The solution correctly compares window sums and only modifies performance if window sum != thresholds. |