## 1176. Diet Plan Performance

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.

**Example 1:**

Input:calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3Output:0Explanation: 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:**

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

**Example 3:**

Input:calories = [6,5,0,0], k = 2, lower = 1, upper = 5Output:0Explanation: 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`

## Rust Solution

```
struct Solution;
impl Solution {
fn update(sum: i32, lower: i32, upper: i32, point: &mut i32) {
if sum < lower {
*point -= 1;
}
if sum > upper {
*point += 1;
}
}
fn diet_plan_performance(calories: Vec<i32>, k: i32, lower: i32, upper: i32) -> i32 {
let mut sum = 0;
let mut point = 0;
let n = calories.len();
let k = k as usize;
for i in 0..k {
sum += calories[i];
}
Self::update(sum, lower, upper, &mut point);
for i in 0..n - k {
sum -= calories[i];
sum += calories[i + k];
Self::update(sum, lower, upper, &mut point);
}
point
}
}
#[test]
fn test() {
let calories: Vec<i32> = vec![1, 2, 3, 4, 5];
let k = 1;
let lower = 3;
let upper = 3;
let res = 0;
assert_eq!(
Solution::diet_plan_performance(calories, k, lower, upper),
res
);
let calories: Vec<i32> = vec![3, 2];
let k = 2;
let lower = 0;
let upper = 1;
let res = 1;
assert_eq!(
Solution::diet_plan_performance(calories, k, lower, upper),
res
);
let calories: Vec<i32> = vec![6, 5, 0, 0];
let k = 2;
let lower = 1;
let upper = 5;
let res = 0;
assert_eq!(
Solution::diet_plan_performance(calories, k, lower, upper),
res
);
}
```

Having problems with this solution? Click here to submit an issue on github.