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 givenindexto 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
50000calls will be made toset,snap, andget. 0 <= index < length0 <= 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 ...
随机推荐
- Java代码中对IP进行白名单验证
来自:https://www.cnblogs.com/shinubi/p/6723003.html public class ipUtil { // IP的正则,这个正则不能验证第一组数字为0的情况 ...
- Windows 有没有办法查看文件被哪个进程占用
经常当我们删除文件时,有时会提示[操作无法完成,因为文件已在另一个程序中打开,请关闭该文件并重试],到底是哪些程序呢? 有时候一个一个找真不是办法,已经被这个问题折磨很久了,今天下决心要把它解决,找到 ...
- 基于 Docker 实现 DevOps 的一些探索
DevOps 介绍 DevOps(Deveplopment 和 Operations 的简称),中译为开发运维一体化,可定义为是一种过程.方法.文化.运动或实践,主要是为了通过一条高度自动化的流水线来 ...
- PowerShell的异常处理办法
$ErrorActionPreference = 'Stop' Try{ # C:\xxx 不存在 Copy-Item C:\xxx -ErrorAction Stop } Catch ...
- 分库分表数据库自增 id
分库分表之后,ID 主键如何处理? 面试题 分库分表之后,id 主键如何处理? 面试官心理分析 其实这是分库分表之后你必然要面对的一个问题,就是 id 咋生成?因为要是分成多个表之后,每个表都是从 1 ...
- Oracle 自定义函数实现列转行效果
在 Oracle 领域,我相信一说到列转行大部分人都会立马想到 WM_CONCAT 函数,我觉得主要是因为该函数比较实用.但事实上 WM_CONCAT 并非官方公开函数,使用会存在一定的风险:函数返回 ...
- Windows 配置网络文件夹映射
mklink /D D:\temp\pythonmxds2 \\192.168.190.186\bigdata\kaoyanmxds
- arcgis js api 4.X 自定义工具按钮
// All material copyright ESRI, All Rights Reserved, unless otherwise specified. // See https://js.a ...
- UWP 使用exe程序
0 添加程序到UWP中 1 添加引用 Windows Desktop Extensions For The UWP 2 修改清单文件(在清单文件上右键查看代码) 2.1 添加xmlns引用 //P ...
- Python转义序列
正则表达式参考:https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html