原题链接在这里: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. Scala字符串插值 - StringContext

    翻译自:STRING INTERPOLATION 简介 自2.10.0版本开始,Scala提供了一种新的机制来根据数据生成字符串:字符串插值.字符串插值允许使用者将变量引用直接插入处理过的字面字符中. ...

  2. 【操作系统之十五】iptables黑白名单、自定义链、网络防火墙、常用动作

    1.黑白名单当链的默认策略为ACCEPT时,链中的规则对应的动作应该为DROP或者REJECT,表示只有匹配到规则的报文才会被拒绝,没有被规则匹配到的报文都会被默认接受,这就是"黑名单&qu ...

  3. django实战(四)--修改数据

    这节我们实现修改数据的功能,惯例,还是先上代码: urls.py urlpatterns=[ path('curd/edit/',views.curd_edit,name='curdedit'), p ...

  4. kubernetes使用阿里云cpfs持久存储

    目录 简介 安装cpfs客户端 kubernetes使用cfs作为持久存储 简介 cpfs的具体介绍可参考这里: https://help.aliyun.com/document_detail/111 ...

  5. NFS客户端挂载失败之authenticated unmount request from

    1.故障现象 客户端挂载时夯住,无反应,无报错,如下图: 2.故障前对挂载目录的操作 发现故障前挂载目录被误删除,后通过备份分为恢复 3.故障排查步骤 .检查客户端及服务端防火墙规则 .检查selin ...

  6. Password file not found:.../jmxremote.password

    jmxremote.password 在jdk/jre/lib/management/下,jmxremote.password.template复制,去掉.template后缀 在配置JMX远程访问的 ...

  7. java如何消除太多的if else判断?

    1.简介 if判断语句是很多编程语言的重要组成部分.但是,若我们最终编写了大量嵌套的if语句,这将使得我们的代码更加复杂和难以维护. 让我们看看能否使用别的方式来做呢. 设计模式是为了更好的代码重用性 ...

  8. 我是如何一步步编码完成万仓网ERP系统的(十)产品库设计 6.属性项和类别关联

    https://www.cnblogs.com/smh188/p/11533668.html(我是如何一步步编码完成万仓网ERP系统的(一)系统架构) https://www.cnblogs.com/ ...

  9. vscode+flutter+win10搭建问题记录

    1.下载安装vscode.flutter sdk.安装vscode相关插件.android sdk,这些网上有教程,比如https://blog.csdn.net/SVNzK/article/deta ...

  10. LearnOpenGL笔记(2)三角形

    这是学习LearnOpenGL CN教程的笔记,包括我遇到的问题和我的烂笔头.文章名与网站小节对应. ------------------------------------分割线---------- ...