-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
MaximumAverageSubarray.I.cpp
48 lines (43 loc) · 1.15 KB
/
MaximumAverageSubarray.I.cpp
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
// Source : https://leetcode.com/problems/maximum-average-subarray-i/description/
// Author : Hao Chen
// Date : 2018-04-19
/***************************************************************************************
*
*
* 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: aximum average is (12-5-6+50)/4 = 51/4 = 12.75
*
*
*
* Note:
*
* 1 k n
* 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++ ) {
sum += nums[i];
}
int s = sum;
for (int i=k; i< nums.size(); i++) {
s += nums[i];
s -= nums[i-k];
if (s > sum) {
sum = s;
}
}
return (double)sum/k;
}
};