Skip to content

Commit 255b9a1

Browse files
authored
Update Diet Plan Performance.java
1 parent 9845f6f commit 255b9a1

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

Easy/Diet Plan Performance.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
class Solution {
2-
int points;
3-
public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
4-
points = 0;
5-
int totalCal = 0;
6-
int start = 0;
7-
for (int i = 0; i < k; i++) {
8-
totalCal += calories[i];
9-
}
10-
for (int i = k; i < calories.length; i++) {
11-
updatePoints(totalCal, lower, upper);
12-
totalCal -= calories[start++];
13-
totalCal += calories[i];
14-
}
15-
updatePoints(totalCal, lower, upper);
16-
return points;
2+
public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
3+
int points = 0;
4+
int totalCalories = 0;
5+
for (int i = 0; i < k - 1; i++) {
6+
totalCalories += calories[i];
177
}
18-
19-
private void updatePoints(int currCal, int lower, int upper) {
20-
if (currCal < lower) {
21-
points--;
22-
}
23-
if (currCal > upper) {
24-
points++;
25-
}
8+
for (int i = k - 1; i < calories.length; i++) {
9+
totalCalories += calories[i];
10+
points += totalCalories < lower ? -1 : 0;
11+
points += totalCalories > upper ? 1 : 0;
12+
totalCalories -= calories[i - k + 1];
2613
}
14+
return points;
15+
}
2716
}

0 commit comments

Comments
 (0)