• 1146. Snapshot Array


    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 given index to be equal to val.
    • int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
    • int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id

    Solution0:

    class SnapshotArray:
    
        def __init__(self, length: int):
            self.array = [[0] for i in range(length)]
            self.indx = [[0] for i in range(length)]
            self.snaps = 0
    
        def set(self, index: int, val: int) -> None:
            snaps = self.snaps
            self.indx[index].append(snaps)
            self.array[index].append(val)
    
        def snap(self) -> int:
            self.snaps += 1
            return self.snaps - 1
    
        def get(self, index: int, snap_id: int) -> int:
    
            if not self.indx[index]:
                return 0
            left = 0
            right = len(self.indx[index]) - 1
            while (left < right):
                mid = left + (right - left)//2 
                if (self.indx[index][mid] > snap_id):
                    right = mid
                else:
                    left = mid + 1
            
            if self.indx[index][left] > snap_id:
                return self.array[index][left-1]
            else:
                return self.array[index][left]

    Solution1:

    class SnapshotArray(object):
    
        def __init__(self, length):
            """
            :type length: int
            """
            self.snaps = 0
            self.store = dict()
            self.store[0] = dict()
            
            
    
        def set(self, index, val):
            """
            :type index: int
            :type val: int
            :rtype: None
            """
            self.store[self.snaps][index] = val
            
    
        def snap(self):
            """
            :rtype: int
            """
            self.snaps += 1
            a = (self.store[self.snaps -1]).copy()
            self.store[self.snaps] = a
            return self.snaps -1
                
    
        def get(self, index, snap_id):
            """
            :type index: int
            :type snap_id: int
            :rtype: int
            """
            if index in self.store[snap_id]:
                return self.store[snap_id][index]
            else:
                return 0
  • 相关阅读:
    C# 控制反转
    控制反转和依赖注入
    C#中使用AOP
    jquery ajax
    python(7)- 小程序练习:循环语句for,while实现99乘法表
    007所谓性格与条件并不是成功的阻碍,懦弱才是
    006学习有可能速成吗
    005自学与有人带着哄着逼着学的不同在于自学是一种成熟的自律
    005单打独斗意味着需要更好地管理自己
    004真正的教育是自我教育,真正的学习是自学
  • 原文地址:https://www.cnblogs.com/beatets/p/12170846.html
Copyright © 2020-2023  润新知