Snapshot Array

Problem Id: 1146 Difficulty: Medium


Intuition

Solution


#!/usr/bin/env python
# coding=utf-8
#
# Author: Lucas
# Date: 2019-08-04 11:41:18


class SnapshotArray:
    def __init__(self, length: int):
        self.current_snap = 0
        self.values = [{0:0} for _ in range(length)]

    def set(self, index: int, val: int) -> None:
        self.values[index][self.current_snap] = val

    def snap(self) -> int:
        self.current_snap += 1
        return self.current_snap - 1

    def get(self, index: int, snap_id: int) -> int:
        for snap in range(snap_id, -1, -1):
            if snap in self.values[index]:
                return self.values[index][snap]


# Your SnapshotArray object will be instantiated and called as such:
# obj = SnapshotArray(length)
# obj.set(index,val)
# param_2 = obj.snap()
# param_3 = obj.get(index,snap_id)