Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions hashmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# // Time Complexity : O(1) for put, get and remove
# // Space Complexity : O(n)
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : NA

class MyHashMap(object):
buckets = 1000
bucket_size=1001

def __init__(self):
self.hashmap=[None] * self.buckets

def hashfunc1(self,key):
return key % self.buckets

def hashfunc2(self,key):
return key // self.buckets


def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
primary_bucket = self.hashfunc1(key)
secondary_index = self.hashfunc2(key)
if self.hashmap[primary_bucket] is None:
self.hashmap[primary_bucket] = [None] * self.bucket_size

self.hashmap[primary_bucket][secondary_index] = value

def get(self, key):
"""
:type key: int
:rtype: int
"""
primary_bucket = self.hashfunc1(key)
secondary_index = self.hashfunc2(key)

if self.hashmap[primary_bucket] is not None:
if self.hashmap[primary_bucket][secondary_index] is not None:
return self.hashmap[primary_bucket][secondary_index]

return -1


def remove(self, key):
"""
:type key: int
:rtype: None
"""
primary_bucket = self.hashfunc1(key)
secondary_index = self.hashfunc2(key)

if self.hashmap[primary_bucket] is not None:
self.hashmap[primary_bucket][secondary_index]=None


# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)
40 changes: 40 additions & 0 deletions queue-using-stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# // Time Complexity : O(1) amortized
# // Space Complexity : O(n)
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : NA

class MyQueue(object):
def __init__(self):
self.stack_in =[]
self.stack_out=[]

def push(self, x):
"""
:type x: int
:rtype: None
"""
self.stack_in.append(x)


def pop(self):
"""
:rtype: int
"""
self.peek()
return self.stack_out.pop()


def peek(self):
"""
:rtype: int
"""
if not self.stack_out:
while self.stack_in:
self.stack_out.append(self.stack_in.pop())
return self.stack_out[-1]

def empty(self):
"""
:rtype: bool
"""
return not bool( self.stack_in or self.stack_out)