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. 【原创】go语言学习(十四)IO操作1

    目录: 格式化输入 格式化输出 终端输入输出背后的原理理 bufio包的使用 命令行参数处理理和urfave/cli使用 格式化输入 1.从终端获取⽤用户的输入 格式化输入 fmt.Scan(a …i ...

  2. 1、套按字及http基础知识之一

    MAC地址:设备到设备之间通信时专用(从源主机到目标主机可能经由N台路由设备)4 IP地址:标记主机到主机之间通信时专用 TCP/UDP :提供进程地址 通过port number来标记 进程地址:用 ...

  3. 小程序web-view的使用,跳转到外部链接~

    先说一下需求,要点击榜单,跳到我们的移动web的项目的榜单页,这个不是小程序的哦,就是网页版的. 榜单的html代码: <view class="nav" hover-cla ...

  4. Java核心复习—— 原子性、有序性与Happens-Before

    一. 产生并发Bug的源头 可见性 缓存导致的可见性问题 原子性 线程切换带来的原子性问题 有序性 编译优化带来的有序性问题 上面讲到了 volatile 与可见性,本章再主要讲下原子性.有序性与Ha ...

  5. 2018-2019-2 (内附jdk与webgoat完整安装教程)《网络对抗技术》Exp9 Web安全基础 Week13 20165233

    Exp9 Web安全基础 目录 一.基础问题 二.实验步骤 实验前准备:jdk与webgoat的安装 实验点一:SQL 命令注入(Command Injection) 数字型注入(Numeric SQ ...

  6. Mininet系列实验(二):Mininet可视化应用

    1 实验目的 该实验通过Mininet学习miniedit可视化操作,可直接在界面上编辑任意想要的拓扑,生成python自定义拓扑脚本,简单方便.在实验过程中,可以了解以下方面的知识: Miniedi ...

  7. LeetCode 560. 和为K的子数组(Subarray Sum Equals K)

    题目描述 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] ...

  8. openrstry 限流 是否有清零逻辑

    openrstry  限流  是否有清零逻辑 https://github.com/openresty/lua-resty-limit-traffic

  9. OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow

    1. EGL概述 EGL 是 OpenGL ES 渲染 API 和本地窗口系统(native platform window system)之间的一个中间接口层,它主要由系统制造商实现. EGL提供如 ...

  10. Intel AI Cloud 使用

    1.申请AI Cloud A ‘training-ready’ hardware like Amazon® EC2, Intel® AI DevCloud, or a GPU-based system ...