Solution: Diet Plan Performance
Explore how to use the sliding window pattern to evaluate a dieter's performance based on calorie intake over every sequence of k days. Learn to efficiently update running sums and adjust performance points by comparing totals against given thresholds. This lesson helps you implement and understand an O(n) time and O(1) space complexity solution for diet tracking problems.
We'll cover the following...
Statement
A dieter consumes calories[i] calories on the i-th day.
Given an integer k, the dieter reviews their calorie intake over every sequence of k consecutive days (from calories[i] to calories[i+k-1] for all 0 <= i <= n-k). For each sequence, they calculate T, the total calories consumed over those k days:
If
Tis less thanlower, the dieter performs poorly and loses 1 point.If
Tis greater thanupper, the dieter performs better and gains 1 point.If
Tis betweenlowerandupper(inclusive), the dieter’s performance is normal, and their points remain the same.
The dieter starts with zero points. Return the total points after the dieter follows this routine for all calories.length days. The total points can be negative.
Constraints
calories.length...