当前位置:网站首页>Daily question · sword finger offer | 041 Average value of sliding window (same as 346) · queue
Daily question · sword finger offer | 041 Average value of sliding window (same as 346) · queue
2022-07-18 20:38:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/qIsx9U/solution/chun-c-by-xun-ge-v-80l5/
source : Power button (LeetCode)
subject

Example
Ideas
Their thinking
The title requires that a sliding window be maintained , At the same time, it is necessary to ensure that the data is advanced and backward , Maintaining the sliding window size also requires first in and last out -> Ring queues are preferred
On each call next You need to make statistics on the data in the sliding window , The sliding window needs to be calculated repeatedly , You can use prefixes and for memorized searches
You need to maintain the size of the sliding window , When new data comes , You need to kick out the advanced data in the queue , Just meet the requirements of ring queue , That is, the window size is maintained , You can also update the data
Code
typedef struct {
double * ans;// The circular queue ,i % size -> Ring queues store data
double prefix;// The prefix and
int inode;// Queue subscript
int asize;// Queue size
bool full;// Is the queue full
} MovingAverage;
/** Initialize your data structure here. */
// Initialize structure
MovingAverage* movingAverageCreate(int size) {
MovingAverage * obj = malloc(sizeof(MovingAverage));
obj->ans = malloc(sizeof(double) * size);
memset(obj->ans, 0, sizeof(double) * size);
obj->inode = 0;
obj->asize = size;
obj->prefix = 0;
obj->full = false;// The queue is under
return obj;
}
double movingAverageNext(MovingAverage* obj, int val) {
if(obj->inode == obj->asize)// Whether the queue is full
{
obj->full = true;
obj->inode = 0;
}
obj->prefix -= obj->ans[obj->inode];// Prefix and subtract the current position first
obj->ans[obj->inode++] = (double)val;// The team
obj->prefix += (double)val;// Add prefix and
if(obj->full == true)// Queue full divide all
{
return obj->prefix / obj->asize;
}
return obj->prefix / obj->inode;
}
void movingAverageFree(MovingAverage* obj) {
free(obj->ans);
free(obj);
}
/**
* Your MovingAverage struct will be instantiated and called as such:
* MovingAverage* obj = movingAverageCreate(size);
* double param_1 = movingAverageNext(obj, val);
* movingAverageFree(obj);
*/
Time and space complexity

边栏推荐
- 2. Markdown instructions
- OpenGL es learning (4) -- projection and rendering
- 这些用例设计题,你在面试时遇到过吗?
- wallys/QCA9880/ WiFi 5 (802.11ac) mini PCIe cards deliver higher Tx power and performance (Sponsored
- Still using system Currenttimemillis() statistics code time-consuming? Too low!
- 单源最短路径
- [word] formula typesetting
- [Xuelang download tutorial] 05 Xuelang download's official packet capture Download
- Realize all weekly buttons of a year
- [uniapp calls wechat Payment] uniapp development applet - call wechat payment
猜你喜欢
![[Xuelang download tutorial] 01 configuration of fiddler for Xuelang Download](/img/0b/f2ccff1084385df29757d03e3cabec.png)
[Xuelang download tutorial] 01 configuration of fiddler for Xuelang Download

这些用例设计题,你在面试时遇到过吗?

MySQL六十六问,两万字+五十图详解!有点六!

Test management knowledge: how to transform from a business expert to a grass-roots Manager

全网首发 nuScenes数据集网盘 + 下载方法

淘特关键词搜索商品API接口(商品列表接口,销量排序接口,商品销量接口)

【学浪下载教程】02学浪下载之Fiddler学浪插件配置

A down jacket live broadcast Gmv soared by 430%. Is this the secret of off-season hot sales?
![[Xuelang download tutorial] 05 Xuelang download's official packet capture Download](/img/76/a8549c50918c355154ed33bc8b50f2.png)
[Xuelang download tutorial] 05 Xuelang download's official packet capture Download

【企业微信自建应用开发】
随机推荐
Splunk configuring multi cluster index
[book club issue 13] +ffmpeg command
Fade in and fade out 1920-500 (8)
淘特商品详情API接口(商品销量接口,商品价格排序接口,回头客常买接口,APP商品详情接口,商品属性接口)
Fade in and fade out 1920-500 (7)
1.2.2-sql injection - SQL injection syntax type - error injection
[Xuelang download tutorial] 03 proxifier settings for Xuelang Download
Dalvik virtual machine and art
【学浪下载教程】01学浪下载之Fiddler的配置
Interface test - verify parameter validity / fault tolerance test
动态内存管理(c语言)
Convolution structure and its calculation
[uniapp calls wechat Payment] uniapp development applet - call wechat payment
报错:cannot read properties of undefined(reading ‘forEach‘)
Multipartfile to Base64
[leetcode] sword finger offer 50 The first character that appears only once
如何使用Fiddler抓包某奇艺小程序视频下载
2. Markdown instructions
[in depth study of 4g/5g/6g topic -38]: urllc-9 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -3-analysis ideas and research methods of urllc Techn
Green install MySQL version 5.7 - configure my INI file considerations