数据流滑动窗口平均值 · 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 ...
随机推荐
- 1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- (转!)大话websocket
邪正看眼鼻,真假看嘴唇,功名看气概,富贵看精神. ---曾国藩<冰鉴> 转自https://www.cnblogs.com/fuqiang88/p/5956363.html 原文http: ...
- 批处理-通过mono把c#编译成dll
::copyright@cjy @echo off ::mcs.exe address set addrMcs=D:\Program Files\Unity\Editor\Data\MonoBleed ...
- 在docker中运行jenkins实现代码自动发布到测试服务器
在docker中运行jenkins 用的镜像是apline版:lts-alpine,并设置正确的时区. docker run --name jenkins_master -d \ -p 8081:80 ...
- Samba 简介
SMB 代表的是服务器消息块 (Server Message Block),它是用于在 Windows 上共享文件的协议的原始名称. CIFS 代表公共 Internet 文件系统 (Common I ...
- 对 Spring 的核心(AOP 和 IOC)的理解(大白话)
Spring 首先它是一个开源而轻量级的框架.其核心容器的主要组件是Bean工厂(BeanFactory).Bean工厂使用控制反转(IOC)模式来降低程序代码之间的耦合度,并提供了面向切面编程(AO ...
- position属性详解
内容: 1.position属性介绍 2.position属性分类 3.relative相对定位 4.absolute绝对定位 5.relative和absolute联合使用进行定位 6.fixed固 ...
- pycharm连接虚拟机
Pycharm需要在版本2017.3.3之后才能连接 通过本地的python解释器运行虚拟机的py文件 需要先配置虚拟环境 配置Ubuntu虚拟环境 # 在VitualBox创建Ubuntu虚拟 ...
- 42. linux下数据库服务启动
进到bin目录运行 emctl start dbconsole oracle@suse92:~> sqlplus /nolog SQL*Plus: Release 9.2.0.4.0 - Pro ...
- DNS配置注意事项 正在连接网络
故障现象 公司规模不是很大,大概有50多台计算机,购买了两台IBM服务器.由于内部使用的某个应用软件需要Windows域的支持,所以在这两台IBM服务器上启用了windows 2000 Server的 ...