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. LPC18xx/43xx OTP Controller driver

    LPC18xx/43xx OTP Controller driver /* * @brief LPC18xx/43xx OTP Controller driver * * @note * Copyri ...

  2. 【Oracle】-【LRU和DBWR】-LRU算法与DBWR中的应用

    Oracle体系结构中经常看到LRU算法,Least Recently Used,也有叫“最近最少使用页面置换算法”,简单讲,Oracle会将内存中最近不用的数据库移出内存以腾出空间来加载另外的数据. ...

  3. ASP.NET Web API实践系列07,获取数据, 使用Ninject实现依赖倒置,使用Knockout实现页面元素和视图模型的双向绑定

    本篇接着上一篇"ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API",尝试获取数据. 在Models文件夹下创 ...

  4. utf-8与utf-8(无BOM)的区别

    BOM——Byte Order Mark,就是字节序标记   在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE ...

  5. java链表知识点总结

    下面是一个Link类定义的一部分.它包含了一些数据和下一个链结点的引用: ? 1 2 3 4 5 class Link {     public int data;     public int id ...

  6. 【python】安装Python 的IDE--PyCharm

    [百度网盘-技术-pycharm破解需要的有安装包和破解jar] ================================================== 安装Tensorflow开发环境 ...

  7. python笔记33-python3连mysql增删改查

    前言 做自动化测试的时候,注册了一个新用户,产生了多余的数据,下次用同一账号就无法注册了,这种情况该怎么办呢? 自动化测试都有个数据准备和数据清理的操作,如果因为此用例产生了多余的数据,就需要数据清理 ...

  8. intellij idea 无法启动或调试 spring-boot

    解决方案一: 原因是因为Working directory没有指定, 并且运行前要手动执行mvn clean install命令才可以.所以导致错误了.希望大家不要犯类似错误. 解决方式二: 看看你的 ...

  9. SEO如何利用百度知道日引流上千IP

    个人小站长.SEO们经常为网站没有流量而发愁,一个没有流量的网站就像一个不喝水的人,迟早得死.没有流量,就没有PV,也就是说你的网站只是 给你一个人看的,那做站有什么意义呢?网站上所发布的内容都是分享 ...

  10. [Android Security] 反编译常用工具

    copy : https://down.52pojie.cn/Tools/Disassemblers/