原题链接在这里:https://leetcode.com/problems/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 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

Constraints:

  • 1 <= length <= 50000
  • At most 50000 calls will be made to setsnap, and get.
  • 0 <= index < length
  • 0 <= snap_id < (the total number of times we call snap())
  • 0 <= val <= 10^9

题解:

Instead of make a copy of each snapshot, which takes a lot of memory space, we could record the state of cell when calling set method.

Have a TreeMap array, each TreeMap maintains the states of a cell.

When calling set, mark current snapshot id with the new value of this cell.

When calling get, try to get the floor entry with given snapshot id.

Time Complexity: SnapshotArray, O(length). set, O(logn). snap, O(1). get, O(logn). n is the number of total entries in arr, the number of previoius set call.

Space: O(n).

AC Java:

 class SnapshotArray {
TreeMap<Integer, Integer> [] arr;
int snapId; public SnapshotArray(int length) {
arr = new TreeMap[length];
for(int i = 0; i<length; i++){
arr[i] = new TreeMap<Integer, Integer>();
arr[i].put(0, 0);
} snapId = 0;
} public void set(int index, int val) {
arr[index].put(snapId, val);
} public int snap() {
return snapId++;
} public int get(int index, int snap_id) {
return arr[index].floorEntry(snap_id).getValue();
}
} /**
* Your SnapshotArray object will be instantiated and called as such:
* SnapshotArray obj = new SnapshotArray(length);
* obj.set(index,val);
* int param_2 = obj.snap();
* int param_3 = obj.get(index,snap_id);
*/

LeetCode 1146. Snapshot Array的更多相关文章

  1. 【leetcode】1146. Snapshot Array

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

  2. 1146. Snapshot Array

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

  3. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  4. Snapshot Array

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

  5. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  6. [LeetCode] Sort Transformed Array 变换数组排序

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  7. [LeetCode] Product of Array Except Self 除本身之外的数组之积

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  8. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  9. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

随机推荐

  1. Qt Quick 基本元素初体验

    Qt Quick 作为 QML 语言的标准库,提供了很多基本元素和控件来帮助我们构建 Qt Quick 应用,这节我们简要地介绍一些 Qt Quick 元素. 一. 基本可视化项 1.1 Item I ...

  2. There is no getter for property named 'id' in 'class java.lang.Integer

    There is no getter for property named 'id' in 'class java.lang.Integer 问题描述: 使用mybatis传入参数, 当参数类型是St ...

  3. thinkphp区间查询、统计查询、SQL直接查询

    区间查询 $data['id']=array(array('gt',4),array('lt',10));//默认关系是(and)并且的关系 //SELECT * FROM `tp_user` WHE ...

  4. c语言课本及pta作业中运用到的程序思维

    c语言课本运用到的程序思维 我个人觉得在写程序的时候,有很多题目会用到我们学过的解决一个程序或者一个问题的方法,把这些方法运用起来,将会使自己更加灵活地解决诸多问题,为今后打下良好地基础. (因为还没 ...

  5. Skywalking总结

    步骤四,完善Agent:你会发现,你在skywalking的Web监控页面看到的项目名称并非你原有的项目名称,而是一个默认的—— Your_ApplicationName.这是因为你还没有配置.打开/ ...

  6. Prometheus 告警分配到指定接收组

    Prometheus 告警分配到指定接收组 route属性用来设置报警的分发策略,它是一个树状结构,按照深度优先从左向右的顺序进行匹配. 主要处理流程:1. 接收到Alert,根据labels判断属于 ...

  7. nginx 查看 并发连接数

    通过命令查看 #netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 17 ESTABLISHE ...

  8. CSS实现水平垂直居中的数种方法整合

    CSS实现水平垂直居中可以说是前端老生常谈的问题了,一般面试官问的时候面试者都会回答出来,但是继续追问还有没有其他方法的时候有可能就说不出来了. 本着学习知识的目的,特在此纪录CSS实现水平垂直居中的 ...

  9. 使用Docker之容器的创建、删除

    假设我们现在拥有以下镜像 1:启动一个简单容器 docker container run        //用于启动一个容器 -it   //是容器具有交互性,并与终端进行连接 命令:docker c ...

  10. One SQL to Rule Them All – an Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables(中英双语)

    文章标题 One SQL to Rule Them All – an Efficient and Syntactically Idiomatic Approach to Management of S ...