-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-1093.cpp
73 lines (69 loc) · 1.93 KB
/
Problem-1093.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
//Problem - 1093
// https://leetcode.com/problems/statistics-from-a-large-sample/
class Solution {
public:
vector<double> sampleStats(vector<int>& count) {
vector <double> ans(5, 0.00000);
if(count.size() == 0)
return ans;
double mean = 0, mode = 0, mx = DBL_MIN, mn = DBL_MAX, mode_num = 0, med = 0;
int cnt = 0;
bool flag = 0;
for(int i = 0; i < count.size(); i++) {
if(count[i] != 0) {
if(!flag){
mn = i;
flag = 1;
}
mx = i;
cnt += count[i];
mean += count[i]*i;
if(count[i] > mode_num) {
mode_num = count[i];
mode = i;
}
}
}
mean = mean/cnt;
if(cnt & 1) {
cnt = floor(cnt/2) + 1;
for(int i = 0; i < count.size(); i++) {
if(count[i]) {
cnt -= count[i];
if(cnt <= 0) {
med = i;
break;
}
}
}
} else {
cnt = floor(cnt/2);
double a, b;
bool check = 0;
for(int i = 0; i < count.size(); i++) {
if(count[i] > 0) {
if(check) {
b = i;
break;
}
cnt -= count[i];
if(cnt < 0) {
a = b = i;
break;
}
if(cnt == 0) {
a = i;
check = 1;
}
}
}
med = (a + b) / 2;
}
ans[0] = mn;
ans[1] = mx;
ans[2] = mean;
ans[3] = med;
ans[4] = mode;
return ans;
}
};