Skip to content

Completed Design-2 #2102

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

Open
wants to merge 1 commit 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
38 changes: 38 additions & 0 deletions problem_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Time Complexity: O(1) for push, O(n) for pop and peek, O(1) for empty
# Space Complexity: O(n) for push, O(1) for pop, peek and empty
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# https://leetcode.com/problems/implement-queue-using-stacks/

# Approach:
# Use two stacks to implement queue. In_stack is used to push elements
# and out_stack is used to pop elements.
# If out_stack is empty, pop all elements from in_stack and push to out_stack.
# Pop elements from out_stack.
# Peek elements from out_stack.
# Check if both stacks are empty to check if queue is empty.


class MyQueue:

def __init__(self):
self.in_stack = []
self.out =[]

def push(self, x: int) -> None:
self.in_stack.append(x)


def pop(self) -> int:
self.peek()
return self.out.pop()

def peek(self) -> int:
if not self.out:
while self.in_stack:
self.out.append(self.in_stack.pop())
return self.out[-1]

def empty(self) -> bool:
return not self.out and not self.in_stack
137 changes: 137 additions & 0 deletions problem_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Time Complexity: Best Case: O(1), Worst Case: O(n) where n is the number of elements in the linked list
# Space Complexity: O(n) where n is the number of elements in the linked list
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No

# https://leetcode.com/problems/design-hashmap/description/

# Approach:
# I used linked list simulation in each bucket of the hashmap.


class Node:
def __init__(self, key=None, value=None, next=None):
self.key = key
self.value = value
self.next = next

class MyHashMap:

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

def get_bucket_index(self, key):
return key % self.size

def put(self, key: int, value: int) -> None:
index = self.get_bucket_index(key)
if not self.buckets[index]:
self.buckets[index] = Node(-1, -1)

prev = self.find_node(index, key)
if prev.next:
prev.next.value = value
else:
prev.next = Node(key, value)

def get(self, key):
index = self.get_bucket_index(key)

if not self.buckets[index]:
return -1

prev = self.find_node(index, key)
return prev.next.value if prev.next else -1

def remove(self, key: int) -> None:
index = self.get_bucket_index(key)

if not self.buckets[index]:
return

prev = self.find_node(index, key)
if prev.next:
prev.next = prev.next.next


def find_node(self, index, key):
prev = self.buckets[index]

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

return prev



# def __init__(self):
# self.size = 1000
# self.buckets = [[] for i in range(self.size)]

# def get_bucket_index(self, key):
# return key % self.size

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

# for i , (k,v) in enumerate(self.buckets[index]):
# if k == key:
# self.buckets[index][i] = (key, value)
# return
# self.buckets[index].append((key, value))

# def get(self, key: int) -> int:
# index = self.get_bucket_index(key)
# for k , v in self.buckets[index]:
# if k == key:
# return v

# return -1

# def remove(self, key: int) -> None:
# index = self.get_bucket_index(key)
# for i , (k,v) in enumerate(self.buckets[index]):
# if k == key:
# del self.buckets[index][i]
# return


# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)

# class MyHashMap:

# def __init__(self):
# self.size = 1000
# self.buckets = [[] for i in range(self.size)]

# def get_bucket_index(self, key):
# return key % self.size

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

# for i , (k,v) in enumerate(self.buckets[index]):
# if k == key:
# self.buckets[index][i] = (key, value)
# return
# self.buckets[index].append((key, value))

# def get(self, key: int) -> int:
# index = self.get_bucket_index(key)
# for k , v in self.buckets[index]:
# if k == key:
# return v

# return -1

# def remove(self, key: int) -> None:
# index = self.get_bucket_index(key)
# for i , (k,v) in enumerate(self.buckets[index]):
# if k == key:
# del self.buckets[index][i]
# return