bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1146. Snapshot Array

1146. Snapshot Array

Leetcode Solutions

Efficient Historical Record Keeping for SnapshotArray

  1. Initialize a list of lists, where each sublist represents the history of changes for an element in the array, with an initial record of [0, 0].
  2. When set(index, val) is called, append a new record [current_snap_id, val] to the history of the specified index.
  3. When snap() is called, increment the current_snap_id and return the previous ID.
  4. When get(index, snap_id) is called, perform a binary search on the history of the specified index to find the record with the greatest snapshot ID that is less than or equal to snap_id, and return the associated value.
UML Thumbnail

Simple Snapshot Array with Full Copies

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...