Problem
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3
class MovingAverage { Queuequeue; int size; int sum; /** Initialize your data structure here. */ public MovingAverage(int size) { this.queue = new LinkedList<>(); this.size = size; this.sum = 0; } public double next(int val) { if (queue.size() == size) { sum -= queue.poll(); } queue.offer(val); sum += val; return (double) sum / queue.size(); } } /** * Your MovingAverage object will be instantiated and called as such: * MovingAverage obj = new MovingAverage(size); * double param_1 = obj.next(val); */
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/77381.html
摘要:科學(xué)計(jì)算與數(shù)據(jù)可視化程序設(shè)計(jì)模塊最重要的一個(gè)特點(diǎn)就是其維數(shù)組對(duì)象即該對(duì)象是一個(gè)快速而靈活的大數(shù)據(jù)集容器。兩行及以上為二維表示數(shù)組各維度大小的元組。 科學(xué)計(jì)算與數(shù)據(jù)可視化1 @(程序設(shè)計(jì)) numpy模塊 Numpy最重要的一個(gè)特點(diǎn)就是其N維數(shù)組對(duì)象(即ndarray)該對(duì)象是一個(gè)快速而靈活的大數(shù)據(jù)集容器。 使用Numpy,開(kāi)發(fā)人員可以執(zhí)行以下操作: 1、數(shù)組的算數(shù)和邏輯運(yùn)算。 2、...
摘要:窗口前進(jìn),刪隊(duì)首元素保證隊(duì)列降序加入當(dāng)前元素下標(biāo)從開(kāi)始,每一次循環(huán)都將隊(duì)首元素加入結(jié)果數(shù)組 Sliding Window Maximum Problem Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration fro...
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examp...
摘要:在數(shù)據(jù)緩沖區(qū)已超過(guò)或?qū)懭腙?duì)列當(dāng)前正忙的任何情況下,將返回。當(dāng)返回值時(shí),背壓系統(tǒng)啟動(dòng),它會(huì)暫停傳入的流發(fā)送任何數(shù)據(jù),并等待消費(fèi)者再次準(zhǔn)備就緒,清空數(shù)據(jù)緩沖區(qū)后,將發(fā)出事件并恢復(fù)傳入的數(shù)據(jù)流。 流中的背壓 在數(shù)據(jù)處理過(guò)程中會(huì)出現(xiàn)一個(gè)叫做背壓的常見(jiàn)問(wèn)題,它描述了數(shù)據(jù)傳輸過(guò)程中緩沖區(qū)后面數(shù)據(jù)的累積,當(dāng)傳輸?shù)慕邮斩司哂袕?fù)雜的操作時(shí),或者由于某種原因速度較慢時(shí),來(lái)自傳入源的數(shù)據(jù)就有累積的趨勢(shì),就像...
閱讀 3771·2021-11-11 10:58
閱讀 2564·2021-09-22 15:43
閱讀 2920·2019-08-30 15:44
閱讀 2292·2019-08-30 13:08
閱讀 1890·2019-08-29 17:28
閱讀 955·2019-08-29 10:54
閱讀 733·2019-08-26 11:46
閱讀 3558·2019-08-26 11:43