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
3 changes: 3 additions & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Created-By: JetBrains Kotlin

Binary file added META-INF/main.kotlin_module
Binary file not shown.
Binary file added MinStack.jar
Binary file not shown.
46 changes: 46 additions & 0 deletions MinStack.kt
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions MyHashSet.kt
Original file line number Diff line number Diff line change
@@ -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<MutableList<Int>> = Array(arraySize) { mutableListOf<Int>() }

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))
}
Binary file added output/META-INF/main.kotlin_module
Binary file not shown.