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
40 changes: 40 additions & 0 deletions HashSet.kt
Original file line number Diff line number Diff line change
@@ -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<Int>() }
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)
*/
40 changes: 40 additions & 0 deletions MinStack.kt
Original file line number Diff line number Diff line change
@@ -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<Pair<Int, Int>>()
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()
*/