maximize the top element of the stack after performing exactly K operations

Pop k-1 elements in k-1 operations. Now for the last operation, either you can pop one more element, or can push one element from the popped elements.

So compare both the cases, if you pop element in kth operation, the element on top should be more than all first k-1 popped elements, otherwise for kth operation push the largest element from k-1 popped elements.

Leave a Comment