|
| 1 | +<h2><a href="https://leetcode.com/problems/snapshot-array/">1146. Snapshot Array</a></h2><h3>Medium</h3><hr><div><p>Implement a SnapshotArray that supports the following interface:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>SnapshotArray(int length)</code> initializes an array-like data structure with the given length. <strong>Initially, each element equals 0</strong>.</li> |
| 5 | + <li><code>void set(index, val)</code> sets the element at the given <code>index</code> to be equal to <code>val</code>.</li> |
| 6 | + <li><code>int snap()</code> takes a snapshot of the array and returns the <code>snap_id</code>: the total number of times we called <code>snap()</code> minus <code>1</code>.</li> |
| 7 | + <li><code>int get(index, snap_id)</code> returns the value at the given <code>index</code>, at the time we took the snapshot with the given <code>snap_id</code></li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p> </p> |
| 11 | +<p><strong class="example">Example 1:</strong></p> |
| 12 | + |
| 13 | +<pre><strong>Input:</strong> ["SnapshotArray","set","snap","set","get"] |
| 14 | +[[3],[0,5],[],[0,6],[0,0]] |
| 15 | +<strong>Output:</strong> [null,null,0,null,5] |
| 16 | +<strong>Explanation: </strong> |
| 17 | +SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3 |
| 18 | +snapshotArr.set(0,5); // Set array[0] = 5 |
| 19 | +snapshotArr.snap(); // Take a snapshot, return snap_id = 0 |
| 20 | +snapshotArr.set(0,6); |
| 21 | +snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5</pre> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong>Constraints:</strong></p> |
| 25 | + |
| 26 | +<ul> |
| 27 | + <li><code>1 <= length <= 5 * 10<sup>4</sup></code></li> |
| 28 | + <li><code>0 <= index < length</code></li> |
| 29 | + <li><code>0 <= val <= 10<sup>9</sup></code></li> |
| 30 | + <li><code>0 <= snap_id < </code>(the total number of times we call <code>snap()</code>)</li> |
| 31 | + <li>At most <code>5 * 10<sup>4</sup></code> calls will be made to <code>set</code>, <code>snap</code>, and <code>get</code>.</li> |
| 32 | +</ul> |
| 33 | +</div> |
0 commit comments