原题链接在这里: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. SpringCloud项目中使用Nacos作为注册中心

    SpringCloud和Nacos的介绍原理在这里就不多说了,百度一大堆,这里就只是记录一下刚开始学习时候项目的使用过程 Nacos-server 我这里是从官网下载的Nacos-server 下载地 ...

  2. go-gin-api 规划目录和参数验证(二)

    概述 首先同步下项目概况: 上篇文章分享了,使用 go modules 初始化项目,这篇文章咱们分享: 规划目录结构 模型绑定和验证 自定义验证器 制定 API 返回结构 废话不多说,咱们开始吧. 规 ...

  3. [转帖]龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上

    龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上 http://news.mydrivers.com/1/663/663122.htm 龙芯是中科院下属的计算机所研发的自主产权国 ...

  4. WPF 精修篇 获取系统颜色和字体样式

    原文:WPF 精修篇 获取系统颜色和字体样式 看效果 <Grid> <Rectangle Fill="{DynamicResource {x:Static SystemCo ...

  5. sql server: 数据库备份时出现-operating-system-error-5拒绝访问

    本文转自:https://blog.csdn.net/ibsfn/article/details/80770855 sql-server 数据库备份时出现-operating-system-error ...

  6. css画三角形原理解析

    <div id="div1"></div><div id="div2"></div><div id=&qu ...

  7. ASP.NET Core使用MongoDB数据库

    环境:Asp.Net Core Mvc 2.2,MongoDB 4.09 参考文档:http://mongodb.github.io/mongo-csharp-driver/ http://mongo ...

  8. Delphi - Logs记录,函数实现MsgDsp

    Logs记录-函数实现MsgDsp 大多数时候,我们不太希望消息以交互的形式出现,这个时候我们可以在窗体上放置一个Memo,然后单独开一个线程进行监视,从而实现把消息实时的显示出来,便于开发者分析. ...

  9. 在centos下安装rar解压.rar压缩包

    CentOS本身不自带rar环境,因此对于rar文件无法直接解压,需要先配置rar环境. 首先需要确定自己的系统是64位还是32位的,通过这个命令: [root@localhost]# uname - ...

  10. 如何设计提高服务API的安全性(一)基础介绍

    场景 现今越来越多公司提供了Sass平台服务,大部分也直接提供API.如快递鸟.微信Api.云服务.如何保证这些服务的安全性是一门重要的课题.如快递跟踪.机票查询等很便捷地影响着我们d的生活,对这些技 ...