-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1093 Statistics from a Large Sample.java
More file actions
52 lines (52 loc) · 1.19 KB
/
1093 Statistics from a Large Sample.java
File metadata and controls
52 lines (52 loc) · 1.19 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public double[] sampleStats(int[] count) {
int min;
for (int i= 0;;i +=1) {
if (count[i] > 0) { min = i; break;}
}
int max;
for (int i=count.length -1;; i-= 1) {
if (count[i] > 0) {max = i; break;}
}
int mode = min;
int modec = count[min];
int c = 0;
long sum = 0;
for (int i= min; i<= max; i+=1) {
int ct = count[i];
c += ct;
sum += ((long) i) * ct;
if (count[i] > modec) {mode = i; modec = count[i];}
}
double mean = sum / (double) c;
int median1 = -1;
int median2;
if (c%2 == 1) {
int k = c / 2 + 1;
int t = 0;
for (int i=min;; i+=1) {
t += count[i];
if (t >= k) {median1=i;median2=i;break;}
}
} else {
int k = c / 2;
int t = 0;
for (int i=0;; i+=1) {
t += count[i];
if (t>=k) {
if (median1 < 0) {
if (t>k) {median1=i;median2=i;break;}
median1 = i;
k += 1;
} else {
median2 = i;
break;
}
}
}
}
double median = (median1 + median2) / 2.0;
double r[] = {min, max, mean, median, mode};
return r;
}
}