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. 51nod 1434

    首先可以得出一个性质:LCM(1,2,3,4,...,N-1,N) 中质因子k的出现的次数为t,则有k^t<=n 根据这个性质我们先筛出素数,然后枚举每个质数,求出对应的k和t,然后找出倍数j( ...

  2. SSH dao层异常 org.hibernate.HibernateException: No Session found for current thread

    解决方法: 在 接口方法中添加 事务注解 即可. public interface IBase<PK extends Serializable, T> { @Transactional v ...

  3. Java 有状态和无状态对象的区别

    无状态会话Bean   无状态就是对于一次操作,不能保存数据.无状态对象(Stateless Bean)是没有实例变量的对象,不能保存数据,是不变类,是线程安全的.例如:  public class ...

  4. C++ STL介绍——简介

    目录 1.什么是STL 2.STL中六大组件 2.1 容器(Container) 2.2 迭代器(Iterator) 2.3 算法(Algorithm) 2.4 仿函数(Functor) 2.5 适配 ...

  5. IDEA使用(03)_git撤回(已经commit未push的)操作

    1.问题来源 日常工作中会遇到 commit 到本地仓库的代码,因为一些原因,需要撤销后再提交到本地,或者需要整合多次 commit,然后 push 到远程仓库. 2.IDEA解决方案 I.在idea ...

  6. html中第一行是什么意思

    html中第一行是什么意思 一.总结 一句话总结: 告诉浏览器,让浏览器得知自己要处理的内容时html 二.html中第一行是什么意思 转自或参考:HTML文件第一行是什么东东_百度知道https:/ ...

  7. legend3---阿里云服务器配置多个网站项目

    legend3---阿里云服务器配置多个网站项目 一.总结 一句话总结: 就是和本机上面的一样,多个域名可以指向同一个ip,配置apache的时候记得ServerName配置域名,不要直接整ip 二. ...

  8. vim 操作命令大全(转)

    1. 关于Vim vim是我最喜欢的编辑器,也是Linux下第二强大的编辑器. 虽然emacs是公认的世界第一,我认为使用emacs并没有使用vi进行编辑来得高效. 如果是初学vi,运行一下vimtu ...

  9. 调用微信公众平台扫一扫示例及解决接口报错 { "errMsg": "config:invalid signature" }

    一.解决报错问题 单从报错信息 invalid signature 就可以看出是 签名signature 有问题, 查了很多资料,终于知道了问题点, 后台让我直接调接口是不对的,签名是根据请求接口的页 ...

  10. Logback 输出 JPA SQL日志 到文件

    Logback 输出 JPA SQL日志 到文件 使用Spring Boot 配置 JPA 时可以指定如下配置在控制台查看执行的SQL语句 spring.jpa.show-sql=true Sprin ...