File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments