HOW CAN I SOLVE THIS USING STACK?
well, have you solved this question ? I think it can be solved in O(n) without using stack as well.
yes i have solved it without using stack…i was just thinking how to solve this using stack…
yes i have applied this approach…
Initially if N = 1, print -1. Here’s a rough algorithm for the rest:
- while input != empty(), S.push(input())
- A.push(S.pop()) while the popped elements are in reverse order
- If S = empty(), print -1
- L <- S.pop()
- O(push(S.pop())) while S != empty()
- print O.pop() while O != empty()
- T <- 9
- O.push(num <- A.pop()) and T <- min(T, num) while A != empty()
- Print T
- While O != empty(), num <- O.pop() and print s.t.:
i. T is not printed the first time it is encountered
ii. Print L when L = min(L, num)
Done! Here’s the C++14 implementation.
Done! Here ’s the C++14 implementation.
Is this the approach you wanted?