Skip to content

Commit 1f37eba

Browse files
authored
Update snapshot-array.cpp
1 parent 8d4fcf5 commit 1f37eba

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

C++/snapshot-array.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class SnapshotArray {
1111
if (!A_.count(index)) {
1212
A_[index].emplace_back(0, 0);
1313
}
14-
A_[index].emplace_back(snap_id_, val);
14+
if (A_[index].back().first == snap_id_) {
15+
A_[index].back().second = val;
16+
} else {
17+
A_[index].emplace_back(snap_id_, val);
18+
}
1519
}
1620

1721
int snap() {
@@ -23,7 +27,7 @@ class SnapshotArray {
2327
A_[index].emplace_back(0, 0);
2428
}
2529
const auto& it = prev(lower_bound(A_[index].cbegin(), A_[index].cend(),
26-
make_pair(snap_id + 1, 0)));
30+
make_pair(snap_id + 1, numeric_limits<int>::min())));
2731
return it->second;
2832
}
2933

@@ -37,7 +41,7 @@ class SnapshotArray {
3741
// Space: O(n)
3842
class SnapshotArray2 {
3943
public:
40-
SnapshotArray(int length) {
44+
SnapshotArray2(int length) {
4145
}
4246

4347
void set(int index, int val) {

0 commit comments

Comments
 (0)