-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatisticsfromaLargeSample.cpp
More file actions
35 lines (34 loc) · 1.15 KB
/
statisticsfromaLargeSample.cpp
File metadata and controls
35 lines (34 loc) · 1.15 KB
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
// Source: https://leetcode.com/problems/statistics-from-a-large-sample/
// Author: Miao Zhang
// Date: 2021-04-08
class Solution {
public:
vector<double> sampleStats(vector<int>& count) {
int minval = 255;
int maxval = 0;
double mode = -1;
int modecnt = 0;
int cnt = 0;
long sums = 0;
map<int, int> m;
for (int i = 0; i <= 255; i++) {
if (!count[i]) continue;
minval = min(minval, i);
maxval = max(maxval, i);
sums += static_cast<long>(i) * count[i];
if (count[i] > modecnt) {
mode = i;
modecnt = count[i];
}
m[cnt += count[i]] = i;
}
for (auto& x: m) cout << x.first << " " << x.second << ",";
auto it1 = m.lower_bound((cnt+1) / 2);
double median = it1->second;
auto it2 = next(it1);
if (cnt % 2 == 0 && it2 != m.end() && it1->first == cnt / 2) {
median = (median + it2->second) / 2;
}
return {static_cast<double>(minval), static_cast<double>(maxval), static_cast<double>(sums) / cnt, median, mode};
}
};