diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 00000000..4b01ed6c --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Created-By: JetBrains Kotlin + diff --git a/META-INF/main.kotlin_module b/META-INF/main.kotlin_module new file mode 100644 index 00000000..3a6b9771 Binary files /dev/null and b/META-INF/main.kotlin_module differ diff --git a/MinStack.jar b/MinStack.jar new file mode 100644 index 00000000..653b26b1 Binary files /dev/null and b/MinStack.jar differ diff --git a/MinStack.kt b/MinStack.kt new file mode 100644 index 00000000..19d8d8de --- /dev/null +++ b/MinStack.kt @@ -0,0 +1,46 @@ +// Time Complexity: O(1) on average +// Space Complexity: O(capacity) where n is the number of elements + +class MinStack() { + + private val capacity: Int = 1_000_000 + private val stack = IntArray(capacity) + + private val mins = IntArray(capacity) + + private var top = -1 + + fun push(`val`: Int) { + if (top == capacity - 1) { + throw StackOverflowError("Stack overflow: capacity=$capacity") + } + stack[++top] = `val` + mins[top] = if (top == 0) `val` else minOf(`val`, mins[top - 1]) + } + + fun pop() { + if (top == -1) { + throw NoSuchElementException("Stack underflow: empty stack") + } + top-- + } + + fun top(): Int { + return stack[top] + } + + fun getMin(): Int { + return mins[top] + } +} + +fun main() { + val minStack = MinStack() + minStack.push(-2) + minStack.push(0) + minStack.push(-3) + println(minStack.getMin()) // return -3 + minStack.pop() + println(minStack.top()) // return 0 + println(minStack.getMin()) // return -2 +} diff --git a/MyHashSet.kt b/MyHashSet.kt new file mode 100644 index 00000000..bf24e048 --- /dev/null +++ b/MyHashSet.kt @@ -0,0 +1,41 @@ +// Time Complexity: O(1) on average for add, remove, and contains operations +// Space Complexity: O(n) where n is the number of unique elements added to the set +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +class MyHashSet() { + + val arraySize = 1000 + val buckets: Array> = Array(arraySize) { mutableListOf() } + + fun hash(key: Int) : Int { + return key % arraySize + } + + fun add(key: Int) { + val index = hash(key) + + if(!buckets[index].contains(key)) { + buckets[index].add(key) + } + } + + fun remove(key: Int) { + val index = hash(key) + buckets[index].remove(key) + } + + fun contains(key: Int): Boolean { + val index = hash(key) + return buckets[index].contains(key) + } +} + + fun main() { + var obj = MyHashSet() + obj.add(89) + obj.add(122323) + obj.add(2) + obj.remove(122323) + obj.remove(12) + println(obj.contains(89)) + } \ No newline at end of file diff --git a/output/META-INF/main.kotlin_module b/output/META-INF/main.kotlin_module new file mode 100644 index 00000000..3a6b9771 Binary files /dev/null and b/output/META-INF/main.kotlin_module differ