File tree 1 file changed +6
-5
lines changed
scripts/algorithms/S/Snapshot Array
1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 366 ms (Top 61.08%) | Memory: 85.5 MB (Top 97.26%)
1
2
class SnapshotArray {
2
3
int timestamp;
3
4
unordered_map<int , vector<int >> toSnaps, toValues;
4
5
public:
5
6
SnapshotArray (int length) {
6
7
timestamp = 0 ;
7
8
}
8
-
9
+
9
10
void set (int index, int val) {
10
11
if (toSnaps.count (index ) == 0 ) {
11
12
// After lower_bound, prevent returning negative lo
@@ -16,18 +17,18 @@ class SnapshotArray {
16
17
// same timestamp -> just update value
17
18
if (toSnaps[index ].back () == timestamp) {
18
19
toValues[index ].back () = val;
19
- }
20
+ }
20
21
// not -> add timestamp and value
21
22
else {
22
23
toSnaps[index ].push_back (timestamp);
23
24
toValues[index ].push_back (val);
24
25
}
25
26
}
26
-
27
+
27
28
int snap () {
28
29
return timestamp++;
29
30
}
30
-
31
+
31
32
int get (int index, int snap_id) {
32
33
// check whether index exists or not
33
34
if (toSnaps.count (index ) == 0 ) return 0 ;
@@ -52,4 +53,4 @@ class SnapshotArray {
52
53
* obj->set(index,val);
53
54
* int param_2 = obj->snap();
54
55
* int param_3 = obj->get(index,snap_id);
55
- */
56
+ */
You can’t perform that action at this time.
0 commit comments