摘要:前言的大樣本統(tǒng)計我們對到之間的整數(shù)進(jìn)行采樣,并將結(jié)果存儲在數(shù)組中就是整數(shù)的采樣個數(shù)。我們以浮點(diǎn)數(shù)數(shù)組的形式,分別返回樣本的最小值最大值平均值中位數(shù)和眾數(shù)。
前言
Weekly Contest 142的 大樣本統(tǒng)計:
解題思路我們對 0 到 255 之間的整數(shù)進(jìn)行采樣,并將結(jié)果存儲在數(shù)組 count 中:count[k] 就是整數(shù) k 的采樣個數(shù)。
我們以 浮點(diǎn)數(shù) 數(shù)組的形式,分別返回樣本的最小值、最大值、平均值、中位數(shù)和眾數(shù)。其中,眾數(shù)是保證唯一的。
我們先來回顧一下中位數(shù)的知識:
如果樣本中的元素有序,并且元素數(shù)量為奇數(shù)時,中位數(shù)為最中間的那個元素;
如果樣本中的元素有序,并且元素數(shù)量為偶數(shù)時,中位數(shù)為中間的兩個元素的平均值。
示例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
計數(shù)表示的眾數(shù)是唯一的
答案與真實值誤差在 10^-5 以內(nèi)就會被視為正確答案
本地難度為中等,首先需要讀懂題目意思,本題的入?yún)?shù)組count其實算是一個壓縮數(shù)據(jù)后的數(shù)組。
我們對 0 到 255 之間的整數(shù)進(jìn)行采樣,并將結(jié)果存儲在數(shù)組 count 中:count[k] 就是整數(shù) k 的采樣個數(shù)。
簡單來說就是,數(shù)組count的第k個元素就是k在壓縮前的數(shù)組中出現(xiàn)count[k]個。以示例1的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]
解壓的過程如下:
第0個元素為0,則解壓后的數(shù)組為[] 第1個元素為4,則解壓后的數(shù)組為[1,1,1,1] 第2個元素為3,則解壓后的數(shù)組為[1,1,1,1,2,2,2] 第3個元素為2,則解壓后的數(shù)組為[1,1,1,1,2,2,2,3,3] 第4個元素為2,則解壓后的數(shù)組為[1,1,1,1,2,2,2,3,3,4,4] ...... 省略后續(xù)步驟
搞清楚count的數(shù)據(jù)特征后,選擇使用TreeMap對count進(jìn)行處理,將有效數(shù)字及其出現(xiàn)個數(shù)存儲起來(有效數(shù)字指的是count[k]不為0的元素)。根據(jù)就是根據(jù)題目要求分別處理以下指標(biāo):
最小值:TreeMap中第一個key
最大值:TreeMap中最后一個key
平均值:TreeMap的key之和除以value之和
中位數(shù):
計算出數(shù)組實際的元素個數(shù)(即value之和)
根據(jù)元素個數(shù)的奇偶性,獲取對應(yīng)的值
眾數(shù):出現(xiàn)次數(shù)最多的數(shù)字,即TreeMap中value最大的鍵值對的key
實現(xiàn)代碼/** * 1093. 大樣本統(tǒng)計 * * @param count * @return */ public double[] sampleStats(int[] count) { // 使用TreeMap有序存儲數(shù)字及其出現(xiàn)次數(shù) TreeMapcountMap = new TreeMap<>(); double[] result = new double[5]; // 總和 double sum = 0L; // 數(shù)字出現(xiàn)總次數(shù) double total = 0L; // 最大出現(xiàn)次數(shù) long maxTimes = 0; // 最小值 double min; // 最大值 double max; // 平均值 double average; // 中位數(shù) double middle = 0; // 眾數(shù),出現(xiàn)次數(shù)最多的數(shù)字 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; // 是否為奇數(shù) boolean odd = total % 2 != 0; // 中位數(shù)索引 int middleIndex = (int) ((total - 1) / 2); int index = -1; Iterator > it = countMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry 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; }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/77875.html
摘要:樣本均值的方差是總體方差的為樣本容量,這個結(jié)論是針對有放回抽樣的。某些情況下配對樣本比較難實現(xiàn),比如藥物雙盲試驗,患者不能既服用安慰劑又服用藥物。樣本方差和總體方差的比值,符合分布。 有放回?無放回? 從總體中隨機(jī)抽取一個容量為n的樣本,當(dāng)樣本容量 n足夠大(通常要求n ≥30)時,無論總體是否符合正態(tài)分布,樣本均值都會趨于正態(tài)分布。期望和總體相同,方差為總體的1/n。這即是中心極限定...
摘要:問題是什么能拿來干什么如何求解深入理解是什么混淆矩陣混淆矩陣是理解大多數(shù)評價指標(biāo)的基礎(chǔ),毫無疑問也是理解的基礎(chǔ)。內(nèi)容的召回往往是根據(jù)的排序而決定的。 問題: AUC是什么 AUC能拿來干什么 AUC如何求解(深入理解AUC) AUC是什么 混淆矩陣(Confusion matrix) 混淆矩陣是理解大多數(shù)評價指標(biāo)的基礎(chǔ),毫無疑問也是理解AUC的基礎(chǔ)。豐富的資料介紹著混淆矩陣的概念,...
摘要:確定分流方案使用各類平臺分配流量。備擇假設(shè)與零假設(shè)相反,即實驗者希望證實的假設(shè)。雖然該數(shù)據(jù)集的統(tǒng)計結(jié)果與支付寶的實際規(guī)模有偏差,但不影響解決方案的適用性。選定統(tǒng)計方法由于樣本較大,故采用檢驗。 ...
閱讀 1010·2023-04-25 19:35
閱讀 2673·2021-11-22 09:34
閱讀 3705·2021-10-09 09:44
閱讀 1731·2021-09-22 15:25
閱讀 2945·2019-08-29 14:00
閱讀 3379·2019-08-29 11:01
閱讀 2608·2019-08-26 13:26
閱讀 1743·2019-08-23 18:08