classSolution:defcontainsNearbyDuplicate(self, nums: List[int], k:int)->bool:
seen =list()
n =len(nums)
i =0while i < n:if nums[i]in seen:
index = seen.index(nums[i])if i - index <= k:returnTrueelse:
seen[index]="inf"
seen.append(nums[i])else:
seen.append(nums[i])
i +=1returnFalse
classMyStack:def__init__(self):"""
Initialize your data structure here.
"""
self.stack =list()defpush(self, x:int)->None:"""
Push element x onto stack.
"""
self.stack.append(x)defpop(self)->int:"""
Removes the element on top of the stack and returns that element.
"""return self.stack.pop()deftop(self)->int:"""
Get the top element.
"""return self.stack[-1]defempty(self)->bool:"""
Returns whether the stack is empty.
"""returnlen(self.stack)<=0# Your MyStack object will be instantiated and called as such:# obj = MyStack()# obj.push(x)# param_2 = obj.pop()# param_3 = obj.top()# param_4 = obj.empty()