Skip to content

Commit 72dcaae

Browse files
committed
Runtime: 366 ms (Top 61.08%) | Memory: 85.5 MB (Top 97.26%)
1 parent 783661b commit 72dcaae

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

scripts/algorithms/S/Snapshot Array/Snapshot Array.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
// Runtime: 366 ms (Top 61.08%) | Memory: 85.5 MB (Top 97.26%)
12
class SnapshotArray {
23
int timestamp;
34
unordered_map<int, vector<int>> toSnaps, toValues;
45
public:
56
SnapshotArray(int length) {
67
timestamp = 0;
78
}
8-
9+
910
void set(int index, int val) {
1011
if (toSnaps.count(index) == 0) {
1112
// After lower_bound, prevent returning negative lo
@@ -16,18 +17,18 @@ class SnapshotArray {
1617
// same timestamp -> just update value
1718
if (toSnaps[index].back() == timestamp) {
1819
toValues[index].back() = val;
19-
}
20+
}
2021
// not -> add timestamp and value
2122
else {
2223
toSnaps[index].push_back(timestamp);
2324
toValues[index].push_back(val);
2425
}
2526
}
26-
27+
2728
int snap() {
2829
return timestamp++;
2930
}
30-
31+
3132
int get(int index, int snap_id) {
3233
// check whether index exists or not
3334
if (toSnaps.count(index) == 0) return 0;
@@ -52,4 +53,4 @@ class SnapshotArray {
5253
* obj->set(index,val);
5354
* int param_2 = obj->snap();
5455
* int param_3 = obj->get(index,snap_id);
55-
*/
56+
*/

0 commit comments

Comments
 (0)