[Algorithm] Circular buffer
You run an e-commerce website and want to record the last
Norderids in a log. Implement a data structure to accomplish this, with the following API:
- record(order_id): adds the order_id to the log
- get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
You should be as efficient with time and space as possible.
It seems like an array would be the perfect fit for this problem. We can just initialize the array to have size N, and index it in constant time. Then, when we record any orders, we can pop off the first order and append it to the end. Getting the ith last order would then just be indexing the array at length - i.
This is one issue with this solution, however: when we have to pop off an element when the array is full, we have to move every other element down by 1. That means record takes O(N) time. How can we improve this?
What we can do to avoid having to moving every element down by 1 is to keep a current index and move it up each time we record something. For get_last, we can simply take current - i to get the appropriate element. Now, both record and get_last should take constant time.
This is actually called Circular buffer:

function CB_log (total) {
let current = 0;
let n = total;
let logs = [];
return {
record (id) {
logs[current] = id;
current = (current + 1) % n;
},
get_last(i) {
if (i > n || !i) {
return null;
}
let idx = current - i;
if (idx >= 0) {
return logs[idx]
} else {
return logs[n - Math.abs(idx)]
}
}
}
}
const log = new CB_log(5);
log.record(41);
log.record(17);
log.record(12);
log.record(78);
log.record(100);//
log.record(1);//
log.record(0);//
console.log(
log.get_last(4) //
)
[Algorithm] Circular buffer的更多相关文章
- Circular Buffer
From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html import java.util ...
- The Bip Buffer - The Circular Buffer with a Twist
Introduction The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping on ...
- boost::Circular Buffer
boost.circular_buffer简介 很多时候,我们需要在内存中记录最近一段时间的数据,如操作记录等.由于这部分数据记录在内存中,因此并不能无限递增,一般有容量限制,超过后就将最开始的数据移 ...
- linux网络编程--Circular Buffer(Ring Buffer) 环形缓冲区的设计与实现【转】
转自:https://blog.csdn.net/yusiguyuan/article/details/18368095 1. 应用场景 网络编程中有这样一种场景:需要应用程序代码一边从TCP/IP协 ...
- boost circular buffer环形缓冲类
Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据. 它是一个与STL兼容的容器,类似于 std::list或std::de ...
- Oracle Redo Log 机制 小结(转载)
Oracle 的Redo 机制DB的一个重要机制,理解这个机制对DBA来说也是非常重要,之前的Blog里也林林散散的写了一些,前些日子看老白日记里也有说明,所以结合老白日记里的内容,对oracle 的 ...
- Monitoring and Tuning the Linux Networking Stack: Receiving Data
http://blog.packagecloud.io/eng/2016/06/22/monitoring-tuning-linux-networking-stack-receiving-data/ ...
- Boost 1.61.0 Library Documentation
http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...
- ffmpeg最全的命令参数
Hyper fast Audio and Video encoderusage: ffmpeg [options] [[infile options] -i infile]... {[outfile ...
随机推荐
- HDU 3970 Paint Chain (博弈,SG函数)
Paint Chain Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 开源 java CMS - FreeCMS2.2 菜单管理
项目地址:http://www.freeteam.cn/ 菜单管理 FreeCMS在设计时定位于面向二次开发友好,所以FreeCMS提供了菜单管理功能.二次开发者能够自由添加新的功能菜单到FreeCM ...
- Using an LPC-Link2 as an LPC4370 evaluation board
https://www.lpcware.com/content/faq/lpcxpresso/using-lpclink2-as-lpc4370-eval As well as being a sta ...
- echarts 去掉网格线
去掉 xAxis : [ splitLine:{ show:false }], yAxis : [ splitLine:{ show:false }]
- Android 数据存储04之Content Provider
Content Provider 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang 1 URI 通用资源标志符(Universal Resource Identif ...
- 关于电商ERP的想法
原文地址: http://www.chinaodoo.net/thread-465-1-1.html 试用了下odoo的淘宝订单处理模块,从整个业务流程上已经打通,如果要求不是很高的话,现有的功能基本 ...
- Fiddler抓包12-AutoResponder返回本地数据(mock)
前言 mock可以说是面试必问的话题的,我第一次接触mock的时候也是一脸懵逼.虽然fiddler工具用了很久,里面的打断点,设置自动返回数据功能都用过. mock说的通俗一点就是模拟返回数据,只是面 ...
- 调用人人网API
大致步骤与上篇调用新浪微博API类似.只是感觉新浪微博的做的更好一些,人人网的非常多要手动操作 与新浪微博类似,先在人人网开放平台http://dev.renren.com/注冊站内应用, 把该填的填 ...
- 使用模拟对象(Mock Object)技术进行测试驱动开发
敏捷开发 敏捷软件开发又称敏捷开发,是一种从上世纪 90 年代开始引起开发人员注意的新型软件开发方法.和传统瀑布式开发方法对比,敏捷开发强调的是在几周或者几个月很短的时间周期,完成相对较小功能,并交付 ...
- Ubuntu 查找文件的方法
1. whereis+文件名 用于程序名的搜索,搜索结果只限于二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s),如果省略参数,则返回所有信息. 2. find / -name ...