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
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要分情况讨论除数,没有想数据结构
[一句话思路]:
用queue的动态长度避免分类讨论
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- queue刚满足等于时就应除,不够警惕,下次注意
- 动态计算时可以初始化为0,不能在方法中重置为0,下次注意
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
API design的题就是在类中声明引用,在方法中和实际的对象发生连接
- queue的实现(具体对象)是linkedlist,都是一条链,不难想象. queue用add也没事
[关键模板化代码]:
先说引用,再说对象
Queue<Integer> q;
int s;
double sum; public MovingAverage(int size) {
q = new LinkedList<Integer>();
s = size;
sum = 0;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class MovingAverage { /** Initialize your data structure here. */
Queue<Integer> q;
int s;
double sum; public MovingAverage(int size) {
q = new LinkedList<Integer>();
s = size;
sum = 0;
} public double next(int val) {
//when the queue just equals the size, poll it out
if (q.size() == s) {
sum -= q.poll();
}
q.add(val);
sum += val;
return sum / q.size();
}
} /**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/
346. Moving Average from Data Stream数据窗口流中位数的数据结构设计的更多相关文章
- [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 (数据流动中的移动平均值)$
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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode ...
- [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 ...
- 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 Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- [Swift]LeetCode346. 从数据流中移动平均值 $ Moving Average from Data Stream
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- Moving Average from Data Stream LT346
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
随机推荐
- PHP应用的CI/CD流程实践与学习:一、PHP运行环境的准备
前言:一直以来想学习与实践一下敏捷开发,之前项目虽说口口声声我们项目是敏捷开发,其实很扯. 敏捷开发如果有持续集成.持续部署的支持,那样开发.测试.运维将节省不少精力. 此系列博客只为记录CI/CD的 ...
- Python中的url编码问题
>>> import urllib >>> a = "PythonTab中文网" >>> a 'PythonTab\xe4\x ...
- ShowDialog窗体的return问题
最近的一个项目里,打开新窗口用到了ShowDialog()这种方式,发现在新窗口做保存操作的时候,保存按钮事件下的程序执行完(无论有没有return)都会关闭子窗口. 网上查了一下,发现大家说的方法在 ...
- 解决近期linux下yum更新出现HTTP Error 404 NOT FOUND错误的办法
本文转载自:http://tech.lezi.com/archives/47 最近两天使用yum的163源,出现404错误 [root@localhost yum.repos.d]# yum make ...
- 【AR实验室】mulberryAR :添加连续图像作为输入
本文转载请注明出处 —— polobymulberry-博客园 0x00 - 前言 之前mulberryAR只能利用手机相机实时捕捉图像作为系统的输入,这也比较符合用户的习惯.但是在开发的过程中,有时 ...
- eclipse插件-easy explore
最近找到一个Eclipse的插件,名字是Easy Explore,是Easy Structs 其 中的一个部分.主要的功能就是在Eclipse里面视图的部分如果看到自己的工程,或者Package,包什 ...
- 怎样用java生成GUID与UUID
GUID是一个128位长的数字,一般用16进制表示.算法的核心思想是结合机器的网卡.当地时间.一个随机数来生成GUID.从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义 ...
- 北京师范大学第十六届程序设计竞赛决赛 C萌萌哒身高差
链接:https://www.nowcoder.com/acm/contest/117/C来源:牛客网 萌萌哒身高差 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...
- Resource interpreted as Document but transferred with MIME type application/json laravel异常请求返回警告
一般情况下,laravel在方法里可以向前端返回数组格式 return [], 框架可以自动将数组转成JSON字符串返回,但浏览器会报MIME类型警告, 如是做APP接口可以忽视该警告: 但在前端aj ...
- 02:Sysbench基准压测(oltp_update_index.lua、oltp_update_non_index.lua)my.cnf
目录 Sysbench 基准压测 my.cnf 一.Sysench测试前准备 1.1.压测环境 二.进行OLTP_update测试 2.1.安装压测工具sysbench 2.2.执行压测 三.执行结果 ...