This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
OlderNewer