You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
3-2 ) Stack Min : How would you 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.
3-3 ) Stack of Plates : Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity. SetOfStacks. push() and SetOfStacks. pop() should behave identically to a single stack (that is, pop() should return the same values as it would if there were just a single stack).
3-5 ) Sort Stack : Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.
# 다시 뒤집어 주기 때문에 큰 수가 위로 오도록defsortStack(stack):
temp=newStack()
whilenotstack.isEmpty():
item=stack.pop()
whilenottemp.isEmpty() andtemp.peek() >item:
stack.push(temp.pop())
temp.push(item)
whilenottemp.isEmpty():
stack.push(temp.pop())
returnstack