数据流滑动窗口平均值 · sliding window average from data stream
[抄题]:
给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值。
MovingAverage m = new MovingAverage(3);
m.next(1) = 1 // 返回 1.00000
m.next(10) = (1 + 10) / 2 // 返回 5.50000
m.next(3) = (1 + 10 + 3) / 3 // 返回 4.66667
m.next(5) = (10 + 3 + 5) / 3 // 返回 6.00000
[暴力解法]:
来一个数就存数组,for 循环最近size个数求和取平均返回。
时间分析:size
空间分析:n
[思维问题]:
不知道为什么要定义全局变量:因为两个函数中都要用。
先提前声明,在函数中分别具体实现。
[一句话思路]:
先进先出,用queue实现就行。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 判断queue尺寸,已经满了,就先拿出来 再放进去。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 方法中只有实现,没有声明。que = new LinkedList<Integer>();即可
[复杂度]:Time complexity: O(n) Space complexity: O(n)
n个点,每个点执行一次。不知道queue都是这样吗?下次注意
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[其他解法]:
前缀和,没有通用性,算了
- 时间分析:方便快速求a数组中某一段的和 前缀和做差法 a[k] + a[k + 1] +... + a[j] = s[j] - s[k -1] 时间复杂度降成o(1)
[Follow Up]:
[LC给出的题目变变变]:
239. Sliding Window Maximum median 一堆数求最值,用堆
773. Sliding Puzzle 最短路,用bfs
[代码风格] :
/ 除号两边要打空格
public class MovingAverage {
/*
* @param size: An integer
*/
private double sum = 0;//
private int size;
private int val;
private Queue<Integer> que;
public MovingAverage(int size) {
this.size = size;
que = new LinkedList<Integer>();
}
/*
* @param val: An integer
* @return:
*/
public double next(int val) {
this.val = val;
this.sum = sum;
sum += val;
if (que.size() == size) {
sum = sum - que.poll();
}
que.offer(val);
return sum / que.size();//
}
}
数据流滑动窗口平均值 · sliding window average from data stream的更多相关文章
- 滑动窗口(Sliding Window)技巧总结
什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...
- [Swift]LeetCode239. 滑动窗口最大值 | Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...
- Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解
题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧 ...
- 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 ...
- codevs4373 窗口==poj2823 Sliding Window
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 53676 Accepted: 15399 ...
- [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 ...
- [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 ...
随机推荐
- python 之 决策树分类算法
发现帮助新手入门机器学习的一篇好文,首先感谢博主!:用Python开始机器学习(2:决策树分类算法) J. Ross Quinlan在1975提出将信息熵的概念引入决策树的构建,这就是鼎鼎大名的ID3 ...
- html_常用技巧总结
============= 博客大全: 脚本之家:http://www.jb51.net/list/list_233_104.htm 红黑联盟: http://www.2cto.com/kf/yid ...
- [UE4]集合:TSet容器
一.TSet<T>是什么 UE4中,除了TArray动态数组外,还提供了各种各样的模板容器.这一节,我们就介绍集合容器——TSet<T>.类似于TArray<T>, ...
- Jenkins Error cloning remote repo 'origin', slave node
使用jenkins pull git上的代码,在job中配置好源码管理后,构建时出现如题错误提示: 网上的资料几乎都是在说SSH的配置问题,因为博主项目建立在本地的git服务器上,所以在源码管理中选择 ...
- 并发工具类(四)线程间的交换数据 Exchanger
前言 JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...
- python对象转字典
1.基础实现 class TestDict: name = "wyb" age = " def __init__(self): self.gender = 'male' ...
- tensorboard-sklearn数据-loss
记录sklearn数据训练时的loss值,用tensorboard可视化 三步骤:红字处 import tensorflow as tf from sklearn.datasets import lo ...
- html调用静态json例子
1.json { "current": 2, "result": "success" } 1.html <!doctype html& ...
- Java 权限框架 Shiro 实战二:与spring集成、filter机制
转自:https://www.cnblogs.com/digdeep/archive/2015/07/04/4620471.html Shiro和Spring的集成,涉及到很多相关的配置,涉及到shi ...
- 机器学习入门-贝叶斯统计语料库的词频.groupby() collections
1..groupby()[].agg(by={}) 2. collections.de...(lambda:1) 统计的单词是语料库中所有的词, 对Dataframe统计单词词频,同时增加一列数据co ...