-
Notifications
You must be signed in to change notification settings - Fork 0
/
643.hpp
50 lines (42 loc) · 1.12 KB
/
643.hpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef LEETCODE_643_HPP
#define LEETCODE_643_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
/*
Given an array consisting of n integers,
find the contiguous subarray of given length k that has the maximum average value.
And you need to output the maximum average value.
Example 1:
Input: [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
Note:
1 <= k <= n <= 30,000.
Elements of the given array will be in the range [-10,000, 10,000].
*/
class Solution {
public:
double findMaxAverage(vector<int> &nums, int k) {
int sum = 0;
for (int i = 0; i < k && i < nums.size(); ++i) {
sum += nums[i];
}
double ave = static_cast<double >(sum) / k;
for (int j = k; j < nums.size(); ++j) {
sum += nums[j];
sum -= nums[j - k];
ave = max(ave, static_cast<double >(sum) / k);
}
return ave;
}
};
#endif //LEETCODE_643_HPP