我们对 0
到 255
之间的整数进行采样,并将结果存储在数组 count
中:count[k]
就是整数 k
的采样个数。
我们以 浮点数 数组的形式,分别返回样本的最小值、最大值、平均值、中位数和众数。其中,众数是保证唯一的。
我们先来回顾一下中位数的知识:
- 如果样本中的元素有序,并且元素数量为奇数时,中位数为最中间的那个元素;
- 如果样本中的元素有序,并且元素数量为偶数时,中位数为中间的两个元素的平均值。
示例 1:
输入: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] 输出:[1.00000,3.00000,2.37500,2.50000,3.00000]
示例 2:
输入: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.00000,4.00000,2.18182,2.00000,1.00000]
提示:
count.length == 256
1 <= sum(count) <= 10^9
- 计数表示的众数是唯一的
- 答案与真实值误差在
10^-5
以内就会被视为正确答案
思路:
首先要搞清输入的格式:
[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]
代表原数组[1, 2, 2, 2, 3, 3, 3, 3]。count[i] = k 就代表有 k 个数值为 i 的数
然后题目需要求最小值、最大值、平均值、中位数和众数。
最小值很简单,count数组从左往右第一个不是0的数的下标就是最小值。
最大值同理,从右往左在count数组里找第一个不是0的数的下标。
平均值就需要先统计原数组里数字的个数 = sum(count),然后计算原数组里所有元素之和 = sum(count[i] * i),除一下即可。
中位数需要找到恰好排在中间的一个数或者两个数,线性扫描count,把每个元素的值用一个变量累加,
如果累加和>总个数的一半,说明中位数已经出现了,
然后再判断一下是只有一个数还是有两个数。
众数就是找到count数组里最大值对应的下标。
class Solution(object):
def sampleStats(self, count):
"""
:type count: List[int]
:rtype: List[float]
"""
s = 0
total_cnt = sum(count)
cnt = 0
avg, median, mean, mean_cnt = 0, 0, 0, 0
min_element, max_element = 0, 0
#找最小值
for i in range(len(count)):
if count[i] != 0:
min_element = i
break
#找最大值
for i in range(len(count) - 1, -1, -1):
if count[i] != 0:
max_element = i
break
#找一共统计了多少个数字
geshu = 0
for i in count:
if i > 0:
geshu += i
find = False
for i, num in enumerate(count):
s += num * i #计算总和
if mean_cnt < num: #找count数组最大值的下标
mean = i
mean_cnt = num
cnt += num #找目前出现了多少个数字
if cnt > total_cnt // 2 and find == False:
if total_cnt % 2: #中位数肯定是一个数
median = i
find = True
else:
if cnt - num == total_cnt // 2: #中位数有两个不同的数
for j in range(i - 1, -1, -1): #往前找上一个数
if count[j] > 0:
median = (i + j) /2.0
find = True
break
else:#中位数有两个相同的数
median = i
find = True
return [min_element, max_element, 1.0 * s /geshu, median, mean ]