Implement a SnapshotArray that supports the following interface:

  • SnapshotArray(int length) initializes an array-like data structure with the given length.  Initially, each element equals 0.
  • void set(index, val) sets the element at the given index to be equal to val.
  • int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
  • int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id

Example 1:

Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5 分析:https://leetcode.com/problems/snapshot-array/

The idea is, the whole array can be large, and we may take the snap tons of times. Instead of record the history of the whole array, we will record the history of each cell. And this is the minimum space that we need to record all information. For each A[i], we will record its history. With a snap_id and a its value. When we want to get the value in history, just binary search the time point.

Complexity Time

SnapshotArray(int length) is O(N) time

set(int index, int val) is O(logSnap)

snap() is O(1)

get(int index, int snap_id) is O(logSnap)

 class SnapshotArray {
TreeMap<Integer, Integer>[] A;
int snap_id = ;
public SnapshotArray(int length) {
A = new TreeMap[length];
for (int i = ; i < length; i++) {
A[i] = new TreeMap<Integer, Integer>();
A[i].put(, );
}
} public void set(int index, int val) {
A[index].put(snap_id, val);
} public int snap() {
return snap_id++;
} public int get(int index, int snap_id) {
return A[index].floorEntry(snap_id).getValue();
}
}

Snapshot Array的更多相关文章

  1. LeetCode 1146. Snapshot Array

    原题链接在这里:https://leetcode.com/problems/snapshot-array/ 题目: Implement a SnapshotArray that supports th ...

  2. 【leetcode】1146. Snapshot Array

    题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...

  3. 1146. Snapshot Array

    Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...

  4. scala位压缩与行情转换二进制

    import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer} import java.nio.charset.Charset import ...

  5. JavaScript权威指南--脚本化文档

    知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...

  6. js-NodeList对象和HTMLCollection对象

    getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...

  7. querySelector()与querySelectorAll()的区别及NodeList和HTMLCollection对象的区别

    querySelector().Document.Element类型均可调用该方法. 当用Document类型调用querySelector()方法时,会在文档元素范围内查找匹配的元素:而当用Elem ...

  8. 节点列表和HTML集合

    getElementsByName()和getElementByTagName()返回的都是NodeList集合. 而document.images和document0.forms的属性为HTMLCo ...

  9. zookeeper 删除snapshot和transaction log的源码解读

    转载请注明源地址http://www.cnblogs.com/dongxiao-yang/p/4910059.html zookeeper具有自动清除快照日志和事务日志的工能,可以在配置文件设置aut ...

随机推荐

  1. C++类的介绍

    最近在学习SLAM,顺便将C++类的知识复习一下.(其中部分官方定义和程序设计方法来源于西北工业大学魏英老师)   1.类的定义: 是用户自定义的数据类型. C++一个类定义的形式如下: class ...

  2. python 字典元素操作

    #字典创建>>> dict2 = { 'abc': 123, 98.6: 37 }>>> dict2[98.6]37>>> dict2[" ...

  3. PHP 之Html标签转义与反转义

    1.htmlentities()函数转义html 2.html_entity_decode()函数反转义html 我这里是用来反转义富文本编辑器的内容

  4. Jenkins 获取 Git 的提交记录(Change Log)

    工作中用 Jenkins 做 iOS 和 Android 的持续集成,之前实现的是当 git 有新代码提交的时候,就会自动编译并上传安装包到蒲公英,然后自动发送QQ群通知或者讨论组通知给相关小伙伴,方 ...

  5. AOP 与 Spring中AOP使用(下)

    AOP通知类型 前置通知 在目标方法执行之前进行操作 UserDao.java public class UserDao { public void add(){ System.out.println ...

  6. TynSerial序列(还原)TClientDataSet

    TynSerial序列(还原)TClientDataSet 可以一次性序列(还原)多个TClientDataSet. 1)TClientDataSet查询数据 procedure TForm1.Qry ...

  7. OpenTK学习笔记(2)-工作窗口的三种方法创建方法(winfrom下类的形式创建)

    参考资料: https://www.codeproject.com/Articles/1167212/OpenGL-with-OpenTK-in-Csharp-Part-Initialize-the- ...

  8. 如何在vue-cli 3.x中使用jquery

    由于项目需求要使用jquery,结果各种引用都不济于事. 最后在网上找到了答案,现把它记录一下,给有需要者. 首先下载 jquery. cnpm install jquery --save-dev 方 ...

  9. Vrms、Vpk、W、dBm、dBW、dBuV、dBm/Hz

    负载阻抗Z 在做这些单位转换前第一个需要提到的就是负载阻抗(Z, Ohm),我们在测试测量中说某个量为上面的某一个单位时候,都包含了一个前提条件,那就是负载阻抗,离开了负载阻抗你说的这些总带有一丝耍流 ...

  10. int 和String之间的相互转换

    int ---> String 1. 和 "" 进行拼接 2. 使用String类中的静态方法valueOf: public static String valueOf(in ...