forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDietPlanPerformance.java
More file actions
37 lines (30 loc) · 899 Bytes
/
DietPlanPerformance.java
File metadata and controls
37 lines (30 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package array;
/**
* Description: https://leetcode.com/problems/diet-plan-performance
* Difficulty: Easy
* Time complexity: O(n)
* Space complexity: O(1)
*/
public class DietPlanPerformance {
public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
int points = 0;
int eaten = 0;
int left = 0;
for (int right = 0; right < calories.length; right++) {
eaten += calories[right];
if (right - left + 1 > k) {
eaten -= calories[left];
left++;
}
if (right >= k - 1) {
points += assessPerformance(eaten, lower, upper);
}
}
return points;
}
private int assessPerformance(int eaten, int lower, int upper) {
if (eaten < lower) return -1;
if (eaten > upper) return 1;
return 0;
}
}