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. LPC1800 and LPC4300 MCUs

    LPC1800 Series microcontrollers At 180 MHz, LPC1800 Series microcontrollers combine the industry's f ...

  2. maria-developers 开发者邮件

    https://lists.launchpad.net/maria-developers/

  3. 看opengl 写代码(4) 画一个圆

    opengl 编程指南 P30 以下代码 是 用 直线 连起来 画一个圆. // circle.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...

  4. h股和L股

  5. ArcEngine二次开发错误编码对照表(转)

    阅读数:3323 每当我们在进行AE开发,出现错误时经常会出现错误代码,但是我们并不知道它到底代表什么意思,这里的而错误编码我们可以对照着找到我们需要的时候常详细信息(问题是,经常还是会出现没有错误编 ...

  6. [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式

    android线程池的理解,晚上在家无事 预习了一下android异步加载的例子,也学习到了一个很重要的东东 那就是线程池+缓存  下面看他们的理解. [size=1.8em]Handler+Runn ...

  7. python测试开发django-34.xadmin管理后台

    前言 django自带的admin后台管理页面有点丑陋,于是网上的大神优化了一版后台管理xadmin,并且开源了,在github开源下载到源码. 注意环境搭配: django 2.1.2 xadmin ...

  8. 深入理解VMware虚拟机网络通信原理

    VMware虚拟机的上网方式有三种:NAT.桥接.仅主机模式,本篇介绍桥接模式和NAT模式. 1.实验环境 博主的实验环境如下: 宿主机操作系统:Windows 7 VMware Workstatio ...

  9. [转] hive0.14-insert、update、delete操作测试

    FROM : http://blog.csdn.net/hi_box/article/details/40820341 首先用最普通的建表语句建一个表: hive>create table te ...

  10. Windows平台Mysql使表名区分大小写

    my.ini 里面的mysqld部分   加入 lower_case_table_names=2 [mysqld] lower_case_table_names=2 port= 3306   注: 1 ...