Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
For 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
 
Idea 1. Sliding window, we need to store elements and poll out the first element when reaching the window size, it looks like queue strucutre fits our needs, it's first-in-first-out.
Time complexity: O(n)
Space complexity: O(k) k is the size of the sliding window
 public class MovingAverage {
private Queue<Integer> queue;
private int capacity;
private double sum;
/** Initialize your data structure here. */
public MovingAverage(int size) {
queue = new ArrayDeque<>();
capacity = size;
sum = 0;
} public double next(int val) {
if(queue.size() == capacity) {
sum -= queue.poll();
}
queue.offer(val);
sum += val;
return sum/queue.size();
} public static void main(String[] args) {
int[] nums = {1, 10, 3, 5};
MovingAverage movingAverage = new MovingAverage(3);
for(int num: nums) {
System.out.println(movingAverage.next(num));
}
}
}

python:

 import queue

 class MovingAverage:
def __init__(self, capacity):
self.capacity = capacity
self.sum = 0
self.window = queue.Queue(capacity) def next(self, num):
if self.window.full():
self.sum -= self.window.get() self.sum += num
self.window.put(num)
return self.sum/self.window.qsize() def test():
test_data = [1, 10, 3, 5]
test_subject = MovingAverage(3)
for num in test_data:
print (test_subject.next(num)) if __name__ == '__main__':
test()

Moving Average from Data Stream LT346的更多相关文章

  1. 346. Moving Average from Data Stream

    /* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...

  2. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  3. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  4. LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)$

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  5. [leetcode]346. Moving Average from Data Stream滑动窗口平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  6. 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计

    [抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...

  7. Moving Average from Data Stream -- LeetCode

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  8. [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  9. 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...

随机推荐

  1. canvas动画---- 太阳、地球、月球

    <div> <canvas id="canvas" width="500" height="500"></ca ...

  2. Tcp/Ip 三次握手与四次挥手

    1. TCP/IP模型 我们一般知道OSI的网络参考模型是分为7层:“应表会传网数物”——应用层,表示层,会话层,传输层,网络层,数据链路层,物理层.而实际的Linux网络层协议是参照了OSI标准,但 ...

  3. django models实际操作中遇到的一些问题

    问题1.将主键id改成自动生成的python3 manage.py migrate时报下面的错误 django.db.utils.InternalError: (1091, "Can't D ...

  4. css3修改滚动条样式

    /*滚动条整体样式*/ /*高宽分别对应横竖滚动条的尺寸*/ .content-box::-webkit-scrollbar{ width: 4px; height: 4px; } /*滚动条里面小方 ...

  5. 6. ZigZag Conversion (字符串的连接)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  6. [剑指Offer]46-把数字翻译成字符串(递归思想,循环实现)

    题意 '0'到'25'翻译成'a'到'z',故一个字符串可以有多种翻译方式,如12258有五种翻译方式. 给定字符串,输出有多少种翻译方式 解题思路 递归思想 计f(i)为以第i个字符开始到原字符串结 ...

  7. day 10 函数名的运用,闭包,迭代器

    函数名的本质 函数名本质上就是函数的内存地址 函数名的五种运用: 1.函数名是一个变量 def func(): print(666) print(func) # 函数的内存地址 <functio ...

  8. css常见问题一

    [1]禁止换行.class {word-break:keep-all;white-space:nowrap;}[2]强制换行.class{word-break:break-all;}普通容器中(Div ...

  9. python之virtualenv

    一 virtualenv简介 virtualenv------用来建立一个虚拟的python环境,一个专属于项目的python环境.用virtualenv 来保持一个干净的环境非常有用 在开发Pyth ...

  10. rsync同步工具的配置与使用

    一.什么是rsync?rsync是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具. rsync官网 http://rsync.samba.org/ 二.rsync的工 ...