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
63 changes: 63 additions & 0 deletions leetcode155.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 3 22:02:35 2026

@author: rishigoswamy

Approach:
----------
This stack supports push, pop, top, and getMin in O(1) time.

The idea is to store previous minimum values inside the same stack.
Whenever a new value is pushed that is smaller than or equal to the
current minimum, we first push the old minimum onto the stack and
then update the minimum to the new value.

This allows us to restore the previous minimum in O(1) time when
the current minimum is popped.

Time Complexity:
----------------
push : O(1)
pop : O(1)
top : O(1)
getMin : O(1)

Space Complexity:
-----------------
O(n), where n is the number of elements in the stack.

"""

class MinStack:

def __init__(self):
self.stack = []
self.min = float('inf')

def push(self, val: int) -> None:
if val <= self.min:
self.stack.append(self.min)
self.min = val
self.stack.append(val)

def pop(self) -> None:
if(self.stack.pop() == self.min):
self.min = self.stack.pop()

def top(self) -> int:
return self.stack[-1]


def getMin(self) -> int:
return self.min



# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
67 changes: 67 additions & 0 deletions leetcode705.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 3 22:01:46 2026

@author: rishigoswamy

Approach:
----------
Since the key range is bounded (0 <= key <= 10^6), we can use a
two-level hashing.

The idea is to split the key into two parts:
- Primary index = key % 1000
- Secondary index = key // 1000

This maps each key uniquely to a position in a 2D boolean table.
The secondary array size is 1001 to handle the maximum key value
(1_000_000 // 1000 = 1000).

Because each (primary, secondary) pair uniquely represents a key,
there are no collisions.

Time Complexity:
----------------
add : O(1)
remove : O(1)
contains : O(1)

Space Complexity:
-----------------
O(n), due to the fixed-size 2D table used for direct addressing.


"""

class MyHashSet:

def __init__(self):
self.primaryArraySize = 1000
self.secondaryArraySize = 1001
self.table = [[False] * self.secondaryArraySize for _ in range(self.primaryArraySize)]

def hashKeys(self, key: int):
return [key % self.primaryArraySize, key // self.primaryArraySize ]

def add(self, key: int) -> None:
primaryKey = self.hashKeys(key)[0]
secondaryKey = self.hashKeys(key)[1]
self.table[primaryKey][secondaryKey] = True

def remove(self, key: int) -> None:
primaryKey = self.hashKeys(key)[0]
secondaryKey = self.hashKeys(key)[1]
self.table[primaryKey][secondaryKey] = False

def contains(self, key: int) -> bool:
primaryKey = self.hashKeys(key)[0]
secondaryKey = self.hashKeys(key)[1]
return self.table[primaryKey][secondaryKey]


# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)