Skip to content

Commit 0069f02

Browse files
author
Adam Lin
committed
Update previous work LeetCode75
1 parent f300938 commit 0069f02

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

643_findMaxAverage.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
double findMaxAverage(vector<int>& nums, int k) {
4+
int s = 0;
5+
double avg = 0;
6+
double sum = 0;
7+
vector<double> result;
8+
9+
for(int i = 0; i < nums.size(); i++){
10+
sum += nums[i];
11+
12+
// when i met index k means i is in the end of the window
13+
// so calculate the avg
14+
if(i >= k - 1){
15+
avg = sum / k;
16+
result.push_back(avg);
17+
18+
// Substrate the first of value in window
19+
sum -= nums[s];
20+
// move the start value to next
21+
s++;
22+
}
23+
24+
}
25+
26+
// max_element returns an iterator pointing to the largest element in the container
27+
//(e.g., a std::vector), not the value itself.
28+
// * is used to access the value pointed to by a pointer or an iterator
29+
return *max_element(result.begin(), result.end());
30+
}
31+
};

0 commit comments

Comments
 (0)