1146. Snapshot Array
Description
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 givenindex
to 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:
1 | Input: ["SnapshotArray","set","snap","set","get"] |
Constraints:
1 <= length <= 5 * 10^4
0 <= index < length
0 <= val <= 10^9
- 0 <= snap_id < (the total number of times we call
snap()
) - At most
5 * 10^4
calls will be made toset
,snap
, andget
.
Hints/Notes
- 2025/02/22 Q2
- binary search
- Leetcode solution
Solution
Language: C++
1 | class SnapshotArray { |