Problem

A dieter consumes calories[i] calories on the i-th day.

Given an integer k, for every consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they look at T , the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]):

  • If T < lower, they performed poorly on their diet and lose 1 point;
  • If T > upper, they performed well on their diet and gain 1 point;
  • Otherwise, they performed normally and there is no change in points.

Initially, the dieter has zero points. Return the total number of points the dieter has after dieting for calories.length days.

Note that the total points can be negative.

Examples

Example 1:

1
2
3
4
5
Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
Output: 0
**Explanation** : Since k = 1, we consider each element of the array separately and compare it to lower and upper.
calories[0] and calories[1] are less than lower so 2 points are lost.
calories[3] and calories[4] are greater than upper so 2 points are gained.

Example 2:

1
2
3
4
Input: calories = [3,2], k = 2, lower = 0, upper = 1
Output: 1
**Explanation** : Since k = 2, we consider subarrays of length 2.
calories[0] + calories[1] > upper so 1 point is gained.

Example 3:

1
2
3
4
5
6
Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5
Output: 0
**Explanation** :
calories[0] + calories[1] > upper so 1 point is gained.
lower <= calories[1] + calories[2] <= upper so no change in points.
calories[2] + calories[3] < lower so 1 point is lost.

Constraints:

  • 1 <= k <= calories.length <= 10^5
  • 0 <= calories[i] <= 20000
  • 0 <= lower <= upper

Solution

Method 1 – Sliding Window

Intuition

We want to efficiently compute the sum of every window of size k. The sliding window technique allows us to update the sum in O(1) time as we move the window.

Approach

  1. Initialize the sum of the first k elements.
  2. For each window, compare the sum to lower and upper, and update points accordingly.
  3. Slide the window by subtracting the element going out and adding the new element coming in.
  4. Repeat for all windows.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) {
        int n = calories.size(), ans = 0, s = 0;
        for (int i = 0; i < k; ++i) s += calories[i];
        if (s < lower) ans--;
        else if (s > upper) ans++;
        for (int i = k; i < n; ++i) {
            s += calories[i] - calories[i - k];
            if (s < lower) ans--;
            else if (s > upper) ans++;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
func dietPlanPerformance(calories []int, k, lower, upper int) int {
    n := len(calories)
    ans, s := 0, 0
    for i := 0; i < k; i++ {
        s += calories[i]
    }
    if s < lower {
        ans--
    } else if s > upper {
        ans++
    }
    for i := k; i < n; i++ {
        s += calories[i] - calories[i-k]
        if s < lower {
            ans--
        } else if s > upper {
            ans++
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
        int n = calories.length, ans = 0, s = 0;
        for (int i = 0; i < k; ++i) s += calories[i];
        if (s < lower) ans--;
        else if (s > upper) ans++;
        for (int i = k; i < n; ++i) {
            s += calories[i] - calories[i - k];
            if (s < lower) ans--;
            else if (s > upper) ans++;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    fun dietPlanPerformance(calories: IntArray, k: Int, lower: Int, upper: Int): Int {
        val n = calories.size
        var ans = 0
        var s = 0
        for (i in 0 until k) s += calories[i]
        if (s < lower) ans--
        else if (s > upper) ans++
        for (i in k until n) {
            s += calories[i] - calories[i - k]
            if (s < lower) ans--
            else if (s > upper) ans++
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def dietPlanPerformance(self, calories: list[int], k: int, lower: int, upper: int) -> int:
        n = len(calories)
        ans = 0
        s = sum(calories[:k])
        if s < lower:
            ans -= 1
        elif s > upper:
            ans += 1
        for i in range(k, n):
            s += calories[i] - calories[i - k]
            if s < lower:
                ans -= 1
            elif s > upper:
                ans += 1
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
impl Solution {
    pub fn diet_plan_performance(calories: Vec<i32>, k: i32, lower: i32, upper: i32) -> i32 {
        let n = calories.len();
        let k = k as usize;
        let mut ans = 0;
        let mut s: i32 = calories[..k].iter().sum();
        if s < lower {
            ans -= 1;
        } else if s > upper {
            ans += 1;
        }
        for i in k..n {
            s += calories[i] - calories[i - k];
            if s < lower {
                ans -= 1;
            } else if s > upper {
                ans += 1;
            }
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    dietPlanPerformance(calories: number[], k: number, lower: number, upper: number): number {
        const n = calories.length;
        let ans = 0;
        let s = 0;
        for (let i = 0; i < k; ++i) s += calories[i];
        if (s < lower) ans--;
        else if (s > upper) ans++;
        for (let i = k; i < n; ++i) {
            s += calories[i] - calories[i - k];
            if (s < lower) ans--;
            else if (s > upper) ans++;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of calories. Each window is processed in O(1) time.
  • 🧺 Space complexity: O(1), as only a few variables are used.