-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathMaximum_Average_Subarray_II.cpp
79 lines (78 loc) · 2.29 KB
/
Maximum_Average_Subarray_II.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// dp, 70/74 passed. Don't know why some testcases showing some deviation
class Solution {
public:
double findMaxAverage(vector<int>& nums, int k) {
int n = (int) nums.size();
if(k > n) { return 0.0; }
long long sum = 0LL;
double resultAvg = 0.0;
for(int i = 0; i < k; i++) {
sum += nums[i];
}
resultAvg = (double)sum / k;
vector<long long> sumDp(n, 0LL);
vector<int> countDp(n, 0);
sumDp[k - 1] = sum;
countDp[k - 1] = k;
for(int i = k; i < n; i++) {
long long sum1 = sumDp[i - 1] + nums[i];
long long sum2 = sum + nums[i] - nums[i - k];
double avg1 = (double)sum1 / (countDp[i - 1] + 1);
double avg2 = (double)sum2 / k;
if(avg1 - avg2 >= 0.001) {
sumDp[i] = sum1;
countDp[i] = countDp[i - 1] + 1;
} else {
sumDp[i] = sum2;
countDp[i] = k;
}
resultAvg = max(resultAvg, max(avg1, avg2));
sum += nums[i];
sum -= nums[i - k];
}
return resultAvg;
}
};
// AC
class Solution {
// return true if mid is larger than maximum average
bool islargerThanAvg(vector<int>& nums, double mid, int k) {
double sum = 0;
for(int i = 0; i < k; i++) {
sum += (nums[i] - mid);
}
if(sum >= 0) {
return false;
}
double last = 0;
for(int i = k; i < nums.size(); i++) {
sum += (nums[i] - mid);
last += (nums[i - k] - mid);
if(last < 0) {
sum -= last;
last = 0;
}
if (sum >= 0) {
return false;
}
}
return true;
}
public:
double findMaxAverage(vector<int>& nums, int k) {
double left = INT_MAX, right = INT_MIN;
for(int num : nums) {
right = max(right, double(num));
left = min(left, double(num));
}
while(right - left > 0.00001) {
double mid = left + (right - left) / 2;
if(islargerThanAvg(nums, mid, k)) {
right = mid;
} else {
left = mid;
}
}
return left;
}
};