Skip to content

Instantly share code, notes, and snippets.

View asethia's full-sized avatar

Arun Sethia asethia

  • United States
View GitHub Profile
@asethia
asethia / MinStack.scala
Created July 30, 2018 15:58
Design a stack which, in addition to push and pop, has a function min which returns the minimum element.
/**
* Design a stack which, in addition to push and pop, has a function min which returns the minimum element.
* Push, pop and min should all operate in 0(1) time.
* Created by Arun Sethia on 7/29/18.
*/
case class MinStack(data: List[Int], min: Int) {
/**
*
* @param elem
* @return
@asethia
asethia / QueueViaStacks.scala
Created July 30, 2018 19:56
Implement a MyQueue class which implements a queue using two stacks.
/**
* Implement a MyQueue class which implements a queue using two stacks.
* Time Complexity
* Enqueue O(1), DeQueue O(n) if dequeue called after each enqueue
* Space complexity O(n)
* Created by Arun Sethia on 7/30/18.
*/
case class QueueViaStacks(stack: List[Int], queue: List[Int] = Nil) {
def enQueue(elem: Int) = QueueViaStacks(elem :: stack, queue)
@asethia
asethia / baseConversion.scala
Created August 9, 2020 21:41
Base conversion - like base 10 to 62 or base 10 to 32
/**
* convert base 10 to given new base
*/
def baseConversion(base: Int, toBeConvert: Int): List[Int] = {
def compute(quotient: Int, acc: List[Int]): List[Int] = {
if(quotient<=0) {
acc.reverse
} else {
val q = quotient / base
val r = quotient % base