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.
Input: calories =[1,2,3,4,5], k =1, lower =3, upper =3Output: 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 =1Output: 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 =5Output: 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.
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.
classSolution {
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--;
elseif (s > upper) ans++;
for (int i = k; i < n; ++i) {
s += calories[i] - calories[i - k];
if (s < lower) ans--;
elseif (s > upper) ans++;
}
return ans;
}
};
classSolution {
publicintdietPlanPerformance(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--;
elseif (s > upper) ans++;
for (int i = k; i < n; ++i) {
s += calories[i]- calories[i - k];
if (s < lower) ans--;
elseif (s > upper) ans++;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
fundietPlanPerformance(calories: IntArray, k: Int, lower: Int, upper: Int): Int {
val n = calories.size
var ans = 0var s = 0for (i in0 until k) s += calories[i]
if (s < lower) ans--elseif (s > upper) ans++for (i in k until n) {
s += calories[i] - calories[i - k]
if (s < lower) ans--elseif (s > upper) ans++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defdietPlanPerformance(self, calories: list[int], k: int, lower: int, upper: int) -> int:
n = len(calories)
ans =0 s = sum(calories[:k])
if s < lower:
ans -=1elif s > upper:
ans +=1for i in range(k, n):
s += calories[i] - calories[i - k]
if s < lower:
ans -=1elif s > upper:
ans +=1return ans
impl Solution {
pubfndiet_plan_performance(calories: Vec<i32>, k: i32, lower: i32, upper: i32) -> i32 {
let n = calories.len();
let k = k asusize;
letmut ans =0;
letmut s: i32= calories[..k].iter().sum();
if s < lower {
ans -=1;
} elseif s > upper {
ans +=1;
}
for i in k..n {
s += calories[i] - calories[i - k];
if s < lower {
ans -=1;
} elseif s > upper {
ans +=1;
}
}
ans
}
}