-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Day 7 | 3/20/20 #100
Comments
1146. Snapshot ArrayBrute Force | Memory Limit ExceededMemory usage needs to be more efficient. In this code snippet, too many unused class SnapshotArray(object):
def __init__(self, length):
self.length = length
self.arr = [0] * self.length
self.snap_id = -1
self.dic = {}
def set(self, index, val):
if index >= 0 and index < len(self.arr):
self.arr[index] = val
def snap(self):
self.snap_id += 1
self.dic[self.snap_id] = self.arr[:]
return self.snap_id
def get(self, index, snap_id):
if snap_id in self.dic:
return self.dic[snap_id][index] Hashmap + DeepcopyThis code snippet still duplicates previous However the time complexity is GREAT
from collections import defaultdict
class SnapshotArray(object):
def __init__(self, length):
self.dic = defaultdict(dict)
self.snap_id = 0
def set(self, index, val):
self.dic[self.snap_id][index] = val
def snap(self):
self.snap_id += 1
self.dic[self.snap_id] = self.dic[self.snap_id - 1].copy()
return self.snap_id -1
def get(self, index, snap_id):
if index in self.dic[snap_id]:
return self.dic[snap_id][index]
else:
return 0 Backward While LoopWe can make this search faster by replacing linear query with a binary search from collections import defaultdict
class SnapshotArray:
def __init__(self, length: int):
self.snap_id = 0
self.history = defaultdict(dict)
def set(self, index: int, val: int) -> None:
self.history[self.snap_id][index] = val
def snap(self) -> int:
self.snap_id += 1
return self.snap_id - 1
def get(self, index: int, snap_id: int) -> int:
copy_id = snap_id
while copy_id > 0 and index not in self.history[copy_id]:
copy_id -= 1
if index in self.history[copy_id]:
return self.history[copy_id][index]
return 0 Backward Binary SearchCode Snippet from @lee215 def __init__(self, n):
self.A = [[[-1, 0]] for _ in xrange(n)]
self.snap_id = 0
def set(self, index, val):
self.A[index].append([self.snap_id, val])
def snap(self):
self.snap_id += 1
return self.snap_id - 1
def get(self, index, snap_id):
i = bisect.bisect(self.A[index], [snap_id + 1]) - 1
return self.A[index][i][1] |
The text was updated successfully, but these errors were encountered: