Snapshot Array
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 givenindexto be equal toval.int snap()takes a snapshot of the array and returns thesnap_id: the total number of times we calledsnap()minus1.int get(index, snap_id)returns the value at the givenindex, at the time we took the snapshot with the givensnap_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的更多相关文章
- LeetCode 1146. Snapshot Array
原题链接在这里:https://leetcode.com/problems/snapshot-array/ 题目: Implement a SnapshotArray that supports th ...
- 【leetcode】1146. Snapshot Array
题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...
- 1146. Snapshot Array
Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...
- scala位压缩与行情转换二进制
import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer} import java.nio.charset.Charset import ...
- JavaScript权威指南--脚本化文档
知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...
- js-NodeList对象和HTMLCollection对象
getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...
- querySelector()与querySelectorAll()的区别及NodeList和HTMLCollection对象的区别
querySelector().Document.Element类型均可调用该方法. 当用Document类型调用querySelector()方法时,会在文档元素范围内查找匹配的元素:而当用Elem ...
- 节点列表和HTML集合
getElementsByName()和getElementByTagName()返回的都是NodeList集合. 而document.images和document0.forms的属性为HTMLCo ...
- zookeeper 删除snapshot和transaction log的源码解读
转载请注明源地址http://www.cnblogs.com/dongxiao-yang/p/4910059.html zookeeper具有自动清除快照日志和事务日志的工能,可以在配置文件设置aut ...
随机推荐
- P2637 第一次,第二次,成交!
题目描述 因为奶牛们的节食运动(奶牛还节食?)给农夫JOHN余下了一大批干草无法处理,所以他准备要开一个拍卖会去出售他的干草.他有N(1<=N<=1000)批干草(每批大约100捆).他的 ...
- vue-cli3.0的记录
页面打包的话,需要在根目录创建一个js文件 vue.confing.js 打包app的话,在dist里面只拿自己需要的静态文件
- Tkinter 之Button标签
一.参数说明 语法 作用 Button(root,text='xxxx') 按钮图标显示内容 Button(root,text='xxxx',height=2) 组件的高度(所占行数) Button( ...
- Gitlab 服务器搭建
一.官网地址 首页:https://about.gitlab.com/ 安装说明:https://about.gitlab.com/installation/ 二.安装命令摘录 实际问题:yum 安装 ...
- 怎么在for循环中新建出不同的list
public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 1, 1, 1, 1, 3, ...
- linux tcp 高并发最大连接数
Linux下高并发socket最大连接数所受的限制问题 1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统 ...
- [IMX6DL] CPU频率调节模式以及降频方法
本文转自http://blog.csdn.net/kris_fei/article/details/51822435 Kernel branch: 3.0.35 CPU的频率调节模式:1. Perfo ...
- Matrix: 利用Matrix来设置ImageView的宽高,使图片能正常显示
在Android中加载ImageView一般都不会给ImageView的宽高设置一个确切的值,一般都是直接写成: <ImageView android:id="@+id/iv_test ...
- 通过Redis的list来实现 Server - Client 的同步通信
Redis实现类似同步方法调用的功能(一) 首先声明,这么干纯粹是为了好玩. 通常我们用Redis主要是为了存储一些数据,由于数据在内存里,所以查询更新很快.同时我们也可以利用 Pub/Sub 功能来 ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_04-Eureka注册中心-将服务注册到Eureka Server
cms相当于客户端 配置客户端的信息 后面加逗号分隔开 50102表示向两台eureka服务上报服务,如果有一台死掉了 那么还可以上另外的一台去注册服务 直接把ip注册到eureka 启动类加注解 重 ...