From c1b12e362dba91b1dd8bb9b54bf536af105e2aeb Mon Sep 17 00:00:00 2001 From: rishigoswamy Date: Tue, 3 Feb 2026 22:14:19 -0800 Subject: [PATCH] Add Python implementations for LeetCode 155 and 705 --- leetcode155.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ leetcode705.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 leetcode155.py create mode 100644 leetcode705.py diff --git a/leetcode155.py b/leetcode155.py new file mode 100644 index 00000000..10c49658 --- /dev/null +++ b/leetcode155.py @@ -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() \ No newline at end of file diff --git a/leetcode705.py b/leetcode705.py new file mode 100644 index 00000000..790ec007 --- /dev/null +++ b/leetcode705.py @@ -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) \ No newline at end of file