-
Notifications
You must be signed in to change notification settings - Fork 3
/
644.cpp
40 lines (39 loc) · 1.14 KB
/
644.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
class Solution {
public:
// mid is too large?
bool criteria(vector<int>& nums, double mid, int k) {
double sum = 0;
double preSum = 0;
double minSum = 0;
int n = nums.size();
for (int i = 0; i < n; ++i) {
double diff = nums[i] - mid;
sum += diff;
if (i >= k) {
preSum += nums[i - k] - mid;
minSum = min(minSum, preSum);
}
if (i >= k - 1) {
double maxAvg = sum - minSum;
if (maxAvg > 0) return false;
}
}
return true;
}
double findMaxAverage(vector<int>& nums, int k) {
double maxVal = INT_MIN;
double minVal = INT_MAX;
for (auto& num : nums) {
maxVal = max(maxVal, (double)num);
minVal = min(minVal, (double)num);
}
double left = minVal;
double right = maxVal;
while (right - left > 0.00001) {
double mid = left + (right - left) / 2;
if (criteria(nums, mid, k)) right = mid;
else left = mid;
}
return left;
}
};