[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 ...
随机推荐
- 在Asp.net core返回PushStream
最近用asp.net core webapi实现了一个实时视频流的推送功能,在Asp.net中,这个是通过PushStreamContent来实现的. 基于对asp.net core的知识,随手写了一 ...
- 使用36-pin的STM32输出VGA, VGA output using a 36-pin STM32
使用36-pin的STM32输出VGA 手头上有个项目需要通过单片机来控制将图像显示在LCD上,在网上搜了一阵子,发现都是使用的FPGA做的, 开始自己对FPGA不是很熟,一直在用的也是ARM系列的, ...
- Java嵌入式数据库H2学习总结(二)——在Web应用程序中使用H2数据库
一.搭建测试环境和项目 1.1.搭建JavaWeb测试项目 创建一个[H2DBTest]JavaWeb项目,找到H2数据库的jar文件,如下图所示: H2数据库就一个jar文件,这个Jar文件里面包含 ...
- C#编程(二十)----------静态类
如果类只包含静态的方法和属性,该类就是静态的.静态类在功能上与使用私有静态构造函数创建的类相同.不能创建静态类的实例.使用关键字static关键字,编译器可以检查用户是否不经意间给类添加了实例成员.如 ...
- Oracle Applications Documentation
Oracle E-Business Suite Documentation Web Library Release 12.2+ Link Download Oracle E-Business Suit ...
- oracle11g忘记sys密码
目 录 1 以管理员身份运行cmd窗口 2 启动sqlplus并敲入一系列命令 3 特别注意 1以管理员身份运行cmd窗口 我安装的是oracle11.2版本,sqlplus.exe所在目录是:I: ...
- List与Array之间互换
1 数组转换为List 调用Arrays类的静态方法asList. asList public static <T> List<T> asList(T... a) Return ...
- 将CAGradientLayer当做mask使用
将CAGradientLayer当做mask使用 效果 源码 https://github.com/YouXianMing/Animations // // CAGradientView.h // M ...
- JAVA Iterator 转成 List
List转到Iterator容易,JDK本身就支持,反过来的实现方式如下:1.使用Apache Common Collections 2.自己实现的方法转换3.Guaa实现转换 方式1: #Apach ...
- 真爱如血第七季/全集True Blood迅雷下载
第七季 True Blood Season 7(2014)看点:由于日本科学家合成了一种人造血,一夜之间,吸血鬼不再是人们心中那富有传奇色彩的怪物,而是能与人类和平相处的朋友.已经播出五年的HBO吸血 ...