You run an e-commerce website and want to record the last N order ids 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的更多相关文章

  1. Circular Buffer

    From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html import java.util ...

  2. 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 ...

  3. boost::Circular Buffer

    boost.circular_buffer简介 很多时候,我们需要在内存中记录最近一段时间的数据,如操作记录等.由于这部分数据记录在内存中,因此并不能无限递增,一般有容量限制,超过后就将最开始的数据移 ...

  4. linux网络编程--Circular Buffer(Ring Buffer) 环形缓冲区的设计与实现【转】

    转自:https://blog.csdn.net/yusiguyuan/article/details/18368095 1. 应用场景 网络编程中有这样一种场景:需要应用程序代码一边从TCP/IP协 ...

  5. boost circular buffer环形缓冲类

    Boost.Circular_buffer维护了一块连续内存块作为缓存区,当缓存区内的数据存满时,继续存入数据就覆盖掉旧的数据. 它是一个与STL兼容的容器,类似于 std::list或std::de ...

  6. Oracle Redo Log 机制 小结(转载)

    Oracle 的Redo 机制DB的一个重要机制,理解这个机制对DBA来说也是非常重要,之前的Blog里也林林散散的写了一些,前些日子看老白日记里也有说明,所以结合老白日记里的内容,对oracle 的 ...

  7. Monitoring and Tuning the Linux Networking Stack: Receiving Data

    http://blog.packagecloud.io/eng/2016/06/22/monitoring-tuning-linux-networking-stack-receiving-data/ ...

  8. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  9. ffmpeg最全的命令参数

    Hyper fast Audio and Video encoderusage: ffmpeg [options] [[infile options] -i infile]... {[outfile ...

随机推荐

  1. 采用ASP.NET IIS 注册工具 (Aspnet_regiis.exe)对web.config实行本地加密

    加密原因:我们通常将一些重要的配置信息写在Web.config里面,其中数据库链接就是这样的信息.将这些数据直接明文显示,显然不太安全. 工具: 采用ASP.NET IIS 注册工具 (Aspnet_ ...

  2. sql server在执行批处理时出现错误。错误消息为: 目录名无效

    今天在客户服务器上的sql server上执行脚本,报错提示“在执行批处理时出现错误.错误消息为:目录名无效”,第一反应就是客户是不是在服务器装了360,因为之前有类似问题,360把数据库的文件给隔离 ...

  3. jQuery 事件方法大全-超全的总结

    jquery经常使用的事件: /*     on     off     hover     blur     change     click     dblclick     focus     ...

  4. JavaScript 实例 | w3cschool菜鸟教程

    JavaScript 实例 | w3cschool菜鸟教程 http://www.w3cschool.cc/js/js-examples.html

  5. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(2)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  6. JavaScript进阶系列07,鼠标事件

    鼠标事件有Keydown, Keyup, Keypress,但Keypress与Keydown和Keyup不同,如果按ctrl, shift, caps lock......等修饰键,不会触发Keyp ...

  7. C#编程(四十三)----------Lambda表达式

    Lambda表达式 案例: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. C#编程(二十二)----------继承的类型

    继承的类型 在面向对象的编程中,有两种截然不同的集成类型:实现继承和接口继承 实现继承:表示一个类型派生于一个基类型,它拥有该基类型的所有成员字段和函数.在实现继承中,派生类型采用基类型的每个函数的实 ...

  9. 设计模式之命令模式(Command)

    from::http://www.cnblogs.com/itTeacher/archive/2012/12/04/2801322.html 命令模式将一个请求封装为一个对象,从而使你可用不同的请求对 ...

  10. Spring Boot新模块devtools

    Spring Boot 1.3中引入了一个新的模块,devtools. 顾名思义,这个模块是为开发者构建的,目的在于加快开发速度. 这个模块包含在最新释出的1.3.M1中. 自动禁用模板缓存 一般情况 ...