LeetCode 1146. Snapshot Array
原题链接在这里: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 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
Constraints:
1 <= length <= 50000
- At most
50000
calls will be made toset
,snap
, andget
. 0 <= index < length
0 <= snap_id <
(the total number of times we callsnap()
)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的更多相关文章
- 【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 ...
- 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 ...
- Snapshot Array
Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [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( ...
- [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 ...
- [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. 这道 ...
- [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 ...
随机推荐
- SVN全局文件过滤规则设置
*/packages */packages/* */.vs/* */.vs */.git/* */.git *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a ...
- PHP的小技巧
PHP的小技巧fdd()[0]函数后面可以直接加数组索引 这样可以省内存占用啦 代码也更简洁
- laravel框架中Job和事件event的解析
本篇文章给大家带来的内容是关于laravel框架中Job和事件event的解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在做项目的时候,一直对Job和Event有个疑惑.感觉两 ...
- Windows安装Nginx需要注意的地方
在使用 Nginx 之前,首先要三连问,它是什么?用来做什么?为什么用它? 这篇文章很好的解答了上面的问题,并补充了什么是正向代理和反向代理以及区别的知识 https://www.cnblogs. ...
- TortoiseSVN客户端更改新的URL和账号
一: 变更SVN地址 右键(TortoiseSVN) → Relocate → 输入你新的URL地址 二:变更账号 TortoiseSVN右键->Setting 进入“Setting”之后,也就 ...
- 【FPGA】Verilog实现交通信号灯
大二数字电路的课程设计中,有一份日常作业使用Xilinx FPGA实现简易交通信号灯,但很可惜当时时间有限,没能最终完成.正好在这一学期选修SOPC设计课程,同样采用了Xilinx FPGA,故打算重 ...
- 一个简单的 ValueTask 的示例
Task 确实有潜在的缺点,特别是对于实例创建很多 并且高吞吐量和性能是关键问题的场景 : Task 是一个类.作为一个类,这意味着任何需要创建一个对象的操作都需要分配一个对象,分配的对象越多, ...
- json工具类(三)——net包
package com.ruoyi.common.utils.json; import java.util.List; import java.util.Map; import net.sf.json ...
- Oracle高危安全漏洞:具有查询权限用户可绕开安全限制进行数据修改
数据库版本 11.2.0.* 检查数据库是否存在此bug的脚本: Oracle用户执行此脚本 #!/bin/bash # Usage: 检查ORACLE数据库是否存在高危安全漏洞(具有查询权限用户可绕 ...
- ASP.NET Core MVC 中两种路由的简单配置
1.全局约定路由 这种方式配置优先级比较低,如果控制器或者方法上标记了特性路由那么优先走特性路由. 当建立好一个mvc项目里,路由都是默认配置好的. 如果建立的是空项目那么需要手动配置: 1.需要在C ...