-
Notifications
You must be signed in to change notification settings - Fork 0
/
1146_Snapshot_Array.java
44 lines (37 loc) · 1.14 KB
/
1146_Snapshot_Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* 1146. Snapshot Array
* Problem Link: https://leetcode.com/problems/snapshot-array/
* Difficulty: Medium
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
class SnapshotArray {
int snapId = 0;
TreeMap<Integer, Integer> [] historyRecords;
public SnapshotArray(int length) {
historyRecords = new TreeMap[length];
for (int i = 0; i < length; i++) {
historyRecords[i] = new TreeMap<Integer, Integer> ();
historyRecords[i].put(0,0);
}
}
public void set(int index, int val) {
historyRecords[index].put(snapId, val);
}
public int snap() {
return snapId++;
}
public int get(int index, int snap_id) {
return (int) historyRecords[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);
*/