Null
• 102

# 1093-大样本统计

## 前言

• 如果样本中的元素有序，并且元素数量为奇数时，中位数为最中间的那个元素；
• 如果样本中的元素有序，并且元素数量为偶数时，中位数为中间的两个元素的平均值。

``````输入：count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

``````输入：count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

1. `count.length == 256`
2. `1 <= sum(count) <= 10^9`
3. `计数表示的众数是唯一的`
4. 答案与真实值误差在 `10^-5` 以内就会被视为正确答案

## 解题思路

``[0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]``

``````第0个元素为0，则解压后的数组为[]

......

``````

• 最小值：`TreeMap`中第一个`key`
• 最大值：`TreeMap`中最后一个`key`
• 平均值：`TreeMap``key`之和除以`value`之和
• 中位数：

1. 计算出数组实际的元素个数（即`value`之和）
2. 根据元素个数的奇偶性，获取对应的值
• 众数：出现次数最多的数字，即`TreeMap``value`最大的键值对的`key`

## 实现代码

``````    /**
* 1093. 大样本统计
*
* @param count
* @return
*/
public double[] sampleStats(int[] count) {
// 使用TreeMap有序存储数字及其出现次数
TreeMap<Integer, Integer> countMap = new TreeMap<>();
double[] result = new double[5];
// 总和
double sum = 0L;
// 数字出现总次数
double total = 0L;
// 最大出现次数
long maxTimes = 0;
// 最小值
double min;
// 最大值
double max;
// 平均值
double average;
// 中位数
double middle = 0;
// 众数，出现次数最多的数字
double mode = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
countMap.put(i, count[i]);
sum = sum + i * count[i];
total += count[i];
if (count[i] > maxTimes) {
maxTimes = count[i];
mode = i;
}
}
}
min = countMap.firstKey().doubleValue();
max = countMap.lastKey().doubleValue();
average = sum / total;
// 是否为奇数
boolean odd = total % 2 != 0;
// 中位数索引
int middleIndex = (int) ((total - 1) / 2);
int index = -1;
Iterator<Map.Entry<Integer, Integer>> it = countMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
int num = entry.getKey();
int times = entry.getValue();
index += times;
if (index > middleIndex) {
middle = num;
break;
} else if (index == middleIndex) {
if (odd) {
middle = num;
break;
} else {
middle = (num + it.next().getKey()) / 2.0;
break;
}
}
}
result[0] = min;
result[1] = max;
result[2] = average;
result[3] = middle;
result[4] = mode;
return result;
}``````

14 人关注
87 篇文章