# Write your MySQL query statement below
571. 给定数字的频率查询中位数
Numbers
表:
+-------------+------+ | Column Name | Type | +-------------+------+ | num | int | | frequency | int | +-------------+------+ num 是这张表的主键(具有唯一值的列)。 这张表的每一行表示某个数字在该数据库中的出现频率。
中位数 是将数据样本中半数较高值和半数较低值分隔开的值。
编写解决方案,解压 Numbers
表,报告数据库中所有数字的 中位数 。结果四舍五入至 一位小数 。
返回结果如下例所示。
示例 1:
输入: Numbers 表: +-----+-----------+ | num | frequency | +-----+-----------+ | 0 | 7 | | 1 | 1 | | 2 | 3 | | 3 | 1 | +-----+-----------+ 输出: +--------+ | median | +--------+ | 0.0 | +--------+ 解释: 如果解压这个 Numbers 表,可以得到 [0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3] ,所以中位数是 (0 + 0) / 2 = 0 。
相似题目
原站题解
pythondata 解法, 执行用时: 232 ms, 内存消耗: 59.6 MB, 提交时间: 2023-10-16 09:58:56
import numpy as np import pandas as pd def median_frequency(numbers: pd.DataFrame) -> pd.DataFrame: # 使用numpy.repeat对numbers的每一个num,重复对应的frequency decompressed_data = np.repeat(numbers['num'], numbers['frequency']) # Calculate the median using numpy median = round(np.median(decompressed_data), 1) # Create the output DataFrame output = pd.DataFrame({'median': [median]}) return output
pythondata 解法, 执行用时: 368 ms, 内存消耗: 59.1 MB, 提交时间: 2023-10-16 09:57:56
import pandas as pd def median_frequency(numbers: pd.DataFrame) -> pd.DataFrame: # 构建一个空列表,目的是把num以该行的frequency延展来,生成一个新的列表 decompressed_data = [] for idx, row in numbers.iterrows(): decompressed_data.extend([row['num']] * row['frequency']) # 计算中位数 median = round(pd.Series(decompressed_data).median(), 1) # 返回dataframe output = pd.DataFrame({'median': [median]}) return output
mysql 解法, 执行用时: 244 ms, 内存消耗: 0 B, 提交时间: 2023-10-16 09:56:19
select round(avg(num),1) as median from (select a.*, sum(frequency) over(order by num) as rnk1, sum(frequency) over(order by num desc) as rnk2, sum(frequency) over() as s from Numbers a) tmp where rnk1>=s/2 and rnk2>=s/2
mysql 解法, 执行用时: 165 ms, 内存消耗: 0 B, 提交时间: 2023-10-16 09:56:06
# Write your MySQL query statement below select avg(num) median from (select num, sum(frequency) over(order by num) asc_accumu, sum(frequency) over(order by num desc) desc_accumu from numbers) t1, (select sum(frequency) total from numbers) t2 where asc_accumu >= total/2 and desc_accumu >=total/2;