Snapshot Array
时间:2019-09-18
本文章向大家介绍Snapshot Array,主要包括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 givenindex
to 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)
1 class SnapshotArray { 2 TreeMap<Integer, Integer>[] A; 3 int snap_id = 0; 4 public SnapshotArray(int length) { 5 A = new TreeMap[length]; 6 for (int i = 0; i < length; i++) { 7 A[i] = new TreeMap<Integer, Integer>(); 8 A[i].put(0, 0); 9 } 10 } 11 12 public void set(int index, int val) { 13 A[index].put(snap_id, val); 14 } 15 16 public int snap() { 17 return snap_id++; 18 } 19 20 public int get(int index, int snap_id) { 21 return A[index].floorEntry(snap_id).getValue(); 22 } 23 }
原文地址:https://www.cnblogs.com/beiyeqingteng/p/11541223.html
- Ajax详解
- Spark部署模式另类详解
- c#之线程总结(一)
- Oracle 常用命令
- 设计模式:适配器模式
- 干货:Sqoop导入导出数据练习
- 腾讯云 CDB : 深入解析 MySQL binlog
- 说说JSON和JSONP( 含jquery例子)
- 用 WPF 写的颜色拾取器
- $(window).load()与$(document).ready()的区别
- silverlight 双击事件
- Silverlight button 图片切换样式
- 【腾讯云CDB】教你玩转MyRocks/RocksDB—STATISTICS与后台线程篇
- Caliburn.Micro学习笔记(一)----引导类和命名匹配规则
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Flink Timer(定时器)机制及实现详解
- android:运行时权限工具类的封装
- Android:非Activity跳转Activity时要加FLAG?
- Java自动化测试(接口鉴权 16)
- Android:Tools命名空间原来是有大用处的
- Android:无线调试就是这么简单
- Java自动化测试(回写与断言 17)
- 一篇就够——Kotlin快速入门
- 微信大牛教你深入了解数据库索引
- SqlServer 资源消耗查询
- Android:检查通知权限并跳转到通知设置界面
- OpenShift应用发布和运维设计
- Android:依赖Module的问题汇总
- Android:加载网图时精确获取图片格式
- Android:8.0中未知来源安装权限变更