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
59 changes: 59 additions & 0 deletions Leetcode232.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Time Complexity:
# push: O(1)
# pop: Amortized O(1), worst case O(n)
# peek: Amortized O(1), worst case O(n)
# empty: O(1)

# Space Complexity: O(n), because all elements are stored across the two stacks.

# Did this code successfully run on LeetCode: Yes

class MyQueue(object):

def __init__(self):
self.inSt = []
self.outSt = []


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


def pop(self):
"""
:rtype: int
"""
if not self.outSt:
while self.inSt:
self.outSt.append(self.inSt.pop())
return self.outSt.pop()


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


def empty(self):
"""
:rtype: bool
"""
return not self.inSt and not self.outSt



# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
79 changes: 79 additions & 0 deletions Leetcode706.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Time Complexity:
# Average: O(1) for put, get, and remove
# Worst case: O(n) if all keys fall into the same bucket

# Space Complexity: O(B + n), where B is the number of buckets and n is the number of key-value pairs

# Did this code successfully run on LeetCode: Yes

# Approach:
# 1. Use an array of buckets and calculate the bucket index using key % bucket_count.
# 2. Each bucket is a Python list that stores [key, value] pairs to handle collisions.
# 3. Search only within the selected bucket for put, get, and remove operations.

class MyHashMap(object):

def __init__(self):
self.buckets = 1009 # A prime number helps distribute keys more evenly
self.storage = [None] * self.buckets


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

def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
index = self.get_hash(key)
if self.storage[index] is None:
self.storage[index] = []

bucket = self.storage[index]
for pair in bucket:
if pair[0] == key:
pair[1] = value
return
bucket.append([key, value])


def get(self, key):
"""
:type key: int
:rtype: int
"""
index = self.get_hash(key)
bucket = self.storage[index]
if bucket is None:
return -1
for pair in bucket:
if pair[0] == key:
return pair[1]
return -1


def remove(self, key):
"""
:type key: int
:rtype: None
"""
index = self.get_hash(key)
bucket = self.storage[index]
if bucket is None:
return -1
for i, pair in enumerate(bucket):
if pair[0] == key:
bucket.pop(i)
if len(bucket) == 0:
self.storage[index] = None
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)