mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 643.cpp
This commit is contained in:
parent
a105fbe75e
commit
a79cd82c9b
22
LeetCode-CN/643.cpp
Normal file
22
LeetCode-CN/643.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
class Solution {
|
||||
public:
|
||||
double findMaxAverage(vector<int>& nums, int k) {
|
||||
int n = nums.size();
|
||||
int sum = 0;
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
sum += nums[i];
|
||||
}
|
||||
int current = sum;
|
||||
int max = current;
|
||||
for (int begin = 1; begin + k <= n; begin++)
|
||||
{
|
||||
current = current - nums[begin - 1] + nums[begin + k - 1];
|
||||
if (current > max)
|
||||
{
|
||||
max = current;
|
||||
}
|
||||
}
|
||||
return (double)max / k;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user