Skip to content
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

Design-2 Done #2044

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
78 changes: 78 additions & 0 deletions hashmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# // Initialize a hashmap of size (10^6 +1) as None. Map key-valuie pair for put.
# // Time Complexity : O(1) for put get and remove
# // Space Complexity : O(1) because no auxialliary space is required
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : Tried using Linkedlists for collision. Could not solve in python. Here there is no hashing or collision detection involved.


# // Your code here along with comments explaining your approach
class MyHashMap:

def __init__(self):
self.size = 1000001 # 10^6 +1 size
self.map = [None] * self.size

def put(self, key: int, value: int) -> None: # O(1)
self.map[key] = value # key-value pair

def get(self, key: int) -> int: # O(1)
if self.map[key] == None: # if key is not present -1 else return value
return -1
else:
return self.map[key]


def remove(self, key: int) -> None: # O(1)
self.map[key] = -1 # reset value to -1


# class MyHashMap:
# class Node:

# def __init__(self, key: int, value: int) -> None:
# self.key = key
# self.value = value
# self.next = None

# def __init__(self):
# self.buckets = 10000
# self.storage = [None] * self.buckets

# def get_primary_hash(self, key: int) -> int:
# return key % self.buckets

# def get_previous(self, head: Node, key: int) -> Node:
# prev = None
# curr = head.next

# while curr and curr.key != key:
# prev = curr
# curr = curr.next

# return prev

# def put(self, key: int, value: int) -> None:
# primary_index = self.get_primary_hash(key)


# if self.storage[primary_index] is None:
# self.storage[primary_index] = self.Node(-1, -1) # Dummy head

# prev = self.get_previous(self.storage[primary_index], key)
# curr = self.storage[primary_index].next

# if curr:
# # Key found, update the value
# if curr.key == key:
# curr.value = value
# return
# new_node = self.Node(key, value)
# new_node.next = self.storage[primary_index].next
# self.storage[primary_index].next = new_node

# # Example usage
# my_hash_map = MyHashMap()
# my_hash_map.put(1, 10)
# my_hash_map.put(2, 20)
# print(my_hash_map.get(1)) # Output: 10
# print(my_hash_map.get(3)) # Output: -1 (key not found)
50 changes: 50 additions & 0 deletions queueUsingStack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# We will use two stacks to implement the queue. A queue is FIFO. there will be an inStack and outStack.
# always push element in inStack
# when outStack is empty we will copy all elements from inStack to outStack then top element is popped. Same for peek
# // Time Complexity : O(1). Perfect O(1) for push. For peek and pop only the first pop which invokes the while loop is O(n) otherwise it is O(1)
# // Space Complexity : O(n) . 2n for inStack and outStack
# // Did this code successfully run on Leetcode : Yes
# // Any problem you faced while coding this : No
class MyQueue:


def __init__(self):
self.inStack = [] # elements go in inStack
self.outStack = [] # elements come out of outStack

def push(self, x: int) -> None:
self.inStack.append(x) # always push in inStack


def pop(self) -> int: # whenever pop is called
if self.outStack==[]: # if outStack is empty
while self.inStack!=[]: # if instack is not empty
self.outStack.append(self.inStack.pop()) # copy all elements from inStack to outStack
return self.outStack.pop() # pop top element from outStack


def peek(self) -> int:
if self.outStack==[]: # same logic as pop
while self.inStack!=[]: #
self.outStack.append(self.inStack.pop())
return self.outStack[-1]

def empty(self) -> bool:
if self.inStack==[] and self.outStack==[]:
return True
else:
return False


# Your MyQueue object will be instantiated and called as such:
obj = MyQueue()
obj.push(4)
obj.push(5)
obj.push(6)
obj.push(7)
param_2 = obj.pop()
param_3 = obj.peek()
param_4 = obj.empty()
print(param_2)
print(param_3)
print(param_4)