[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 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
给一个整数流和一个窗口,计算在给定大小的窗口里的数字的平均值。
解法:队列queue,用一个queue记录进入窗口的整数。当流进窗口的整数不足时,计算所有窗口内的数字和返回,当进入窗口的整数多于窗口大小时,移除最先进入窗口的整数,新的整数进入queue,然后计算窗口内的整数和。
Java:
public class MovingAverage {
private double previousSum = 0.0;
private int maxSize;
private Queue<Integer> currentWindow;
/** Initialize your data structure here. */
public MovingAverage(int size) {
currentWindow = new LinkedList<Integer>();
maxSize = size;
}
public double next(int val) {
if(currentWindow.size()==maxSize){
previousSum -= currentWindow.remove();
}
currentWindow.add(val);
previousSum += val;
return previousSum/currentWindow.size();
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/
Java:
public class MovingAverage {
LinkedList<Integer> queue;
int size;
double avg;
/** Initialize your data structure here. */
public MovingAverage(int size) {
this.queue = new LinkedList<Integer>();
this.size = size;
}
public double next(int val) {
if(queue.size()<this.size){
queue.offer(val);
int sum=0;
for(int i: queue){
sum+=i;
}
avg = (double)sum/queue.size();
return avg;
}else{
int head = queue.poll();
double minus = (double)head/this.size;
queue.offer(val);
double add = (double)val/this.size;
avg = avg + add - minus;
return avg;
}
}
}
Python:
# Time: O(1)
# Space: O(w)
from collections import deque class MovingAverage(object):
def __init__(self, size):
"""
Initialize your data structure here.
:type size: int
"""
self.__size = size
self.__sum = 0
self.__q = deque([]) def next(self, val):
"""
:type val: int
:rtype: float
"""
if len(self.__q) == self.__size:
self.__sum -= self.__q.popleft()
self.__sum += val
self.__q.append(val)
return 1.0 * self.__sum / len(self.__q) # Your MovingAverage object will be instantiated and called as such:
# obj = MovingAverage(size)
# param_1 = obj.next(val)
C++:
class MovingAverage {
public:
MovingAverage(int size) {
this->size = size;
sum = 0;
}
double next(int val) {
if (q.size() >= size) {
sum -= q.front(); q.pop();
}
q.push(val);
sum += val;
return sum / q.size();
}
private:
queue<int> q;
int size;
double sum;
};
All LeetCode Questions List 题目汇总
[LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值的更多相关文章
- [LeetCode] Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 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 ...
- [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 ...
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- 【LeetCode】346. Moving Average from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
- 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计
[抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- 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 ...
- Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
随机推荐
- CSP-S 2019提高组训练 服务器需求
时间限制:C/C++ 3秒 空间限制:C/C++ 262144K 题目描述 小多计划在接下来的n天里租用一些服务器,所有的服务器都是相同的.接下来n天中,第i天需要\(a_i\)台服务器工作,每台服务 ...
- 初学Django基础02 ORM操作
django的ORM操作 之前我们知道了models.py这个文件,这个文件是用来读取数据结构的文件,每次操作数据时都走这个模块 常用字段 AutoField int自增列,必须填入参数 primar ...
- Dynamics 365 安装问题——无法访MSCRM_CONFIG数据库
1. 问题 2. 原因 出现此问题的一个或多个下列条件都为真︰ 在多务器中安装 Microsoft Dynamics 365.然后,直接在运行 Microsoft SQL Server 的服务器上 ...
- MapReduce如何解决数据倾斜?
数据倾斜是日常大数据查询中隐形的一个BUG,遇不到它时你觉得数据倾斜也就是书本博客上的一个无病呻吟的偶然案例,但当你遇到它是你就会懊悔当初怎么不多了解一下这个赫赫有名的事故. https://www. ...
- Lightning Web Components 组件样式(四)
要将样式与组件进行绑定,需要创建一个同名的样式文件,这样样式将会自动应用到组件 在组件中定义的样式的作用域是属于组件的,这样允许组件可以在不同的上下文中可以复用, 可以阻止其他组件的样式的复写 css ...
- check_monitor
#! /bin/bash # 声明agent配置文件路径 CONF=/etc/sdata/zabbix/zabbix_agentd.conf #CONF=/tmp/zabbix_agentd.conf ...
- Binding a Xamarin.Forms WebView to ReactiveUI View Model using Custom Type Converters
引用:https://jamilgeor.com/binding-a-xamarin-forms-webview-to-reactiveui-view-model-using-custom-type- ...
- 46、Spark SQL工作原理剖析以及性能优化
一.工作原理剖析 1.图解 二.性能优化 1.设置Shuffle过程中的并行度:spark.sql.shuffle.partitions(SQLContext.setConf()) 2.在Hive数据 ...
- IDEA中用mybatis插件生成逆向工程
目录 maven项目 在resources目录下新建generatorConfig.xml文件 在resources目录下新建config.properties文件 运行 maven项目 <?x ...
- linux(deepin) 下隐藏firefox标题栏
1. 右上角菜单 -> 定制 -> 左下角 "标题栏" 取消打钩 2. 如果上面无法解决,在firefox的启动前插入一个环境变量,具体修改 /usr/share/ap ...