-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1093.Statistics-from-a-Large-Sample.java
64 lines (55 loc) · 1.68 KB
/
1093.Statistics-from-a-Large-Sample.java
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
// https://leetcode.com/problems/statistics-from-a-large-sample/
//
// algorithms
// Medium (40.40%)
// Total Accepted: 3,208
// Total Submissions: 7,940
class Solution {
public double[] sampleStats(int[] count) {
int minimum = -1, maximum = -1, num = 0, maxCount = -1, mode = -1;
long sum = 0;
HashMap<Integer, Integer> map = new HashMap<>();
ArrayList<Integer> l = new ArrayList<>();
for (int i = 0; i < 256; i++) {
if (count[i] != 0) {
num += count[i];
sum += (count[i] * i);
if (minimum == -1) {
minimum = i;
}
if (count[i] > maxCount) {
maxCount = count[i];
mode = i;
}
maximum = i;
l.add(num - 1);
map.put(num - 1, i);
}
}
double[] res = new double[5];
res[0] = (double) minimum;
res[1] = (double) maximum;
res[2] = (double) sum / num;
res[4] = (double) mode;
int i = 0, j = l.size() - 1, m = -1;
int target = num / 2;
while (i <= j) {
m = i + (j - i) / 2;
if (l.get(m) < target) {
i = m + 1;
} else {
j = m - 1;
}
}
if ((num & 1) == 1) {
res[3] = (double) map.get(l.get(j + 1));
} else {
if (target == l.get(j) + 1) {
res[3] = (double) (map.get(l.get(j + 1)) + map.get(l.get(j))) / 2;
} else {
res[3] = (double) map.get(l.get(j + 1));
}
}
return res;
}
}