From cbd264e494794f60b207908389ceb244fb37c334 Mon Sep 17 00:00:00 2001 From: Balaviknesh Sekar Date: Tue, 3 Feb 2026 17:39:08 -0600 Subject: [PATCH] Added solutions --- HashSet.kt | 40 ++++++++++++++++++++++++++++++++++++++++ MinStack.kt | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 HashSet.kt create mode 100644 MinStack.kt diff --git a/HashSet.kt b/HashSet.kt new file mode 100644 index 00000000..51edf37f --- /dev/null +++ b/HashSet.kt @@ -0,0 +1,40 @@ +//Time complexity: O(1) for add, remove, and contains operations +//Space complexity: O(1) for add, remove, and contains operations for hashSet o(n) where n is the size of the hashSet + +class MyHashSet() { + + private val hashSet = Array(1000000) { mutableListOf() } + private var size = 0 + + private fun hashIndex(key: Int): Int { + return key % 1000000 + } + + fun add(key: Int) { + if(!contains(key)) { + size += 1 + hashSet[hashIndex(key)].add(key) + } + } + + fun remove(key: Int) { + if(contains(key)) { + size -= 1 + hashSet[hashIndex(key)].remove(key) + } + } + + fun contains(key: Int): Boolean { + return hashSet[hashIndex(key)].contains(key) + } +} + + + +/** + * Your MyHashSet object will be instantiated and called as such: + * var obj = MyHashSet() + * obj.add(key) + * obj.remove(key) + * var param_3 = obj.contains(key) + */ \ No newline at end of file diff --git a/MinStack.kt b/MinStack.kt new file mode 100644 index 00000000..bde07098 --- /dev/null +++ b/MinStack.kt @@ -0,0 +1,40 @@ +//Time Complexity: O(1) for all operations +//Space Complexity: O(n) where n is the number of elements in the stack + + +class MinStack() { + + val stack = mutableListOf>() + var size = 0 + + fun push(`val`: Int) { + val currentMin = if (size == 0) `val` else minOf(stack[size - 1].second, `val`) + stack.add(Pair(`val`, currentMin)) + size += 1 + } + + fun pop() { + if (size != 0) { + stack.removeAt(size - 1) + size -= 1 + } + } + + fun top(): Int { + return stack[size - 1].first + } + + fun getMin(): Int { + return stack[size - 1].second + } + +} + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = MinStack() + * obj.push(`val`) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.getMin() + */ \ No newline at end of file