Skip to content

Commit 663322e

Browse files
authored
Create diet-plan-performance.cpp
1 parent 0b817f6 commit 663322e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

C++/diet-plan-performance.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) {
7+
int total = accumulate(calories.cbegin(), calories.cbegin() + k, 0);
8+
int result = int(total > upper) - int(total < lower);
9+
for (int i = k; i < calories.size(); ++i) {
10+
total += calories[i] - calories[i - k];
11+
result += int(total > upper) - int(total < lower);
12+
}
13+
return result;
14+
}
15+
};

0 commit comments

Comments
 (0)