You are given an empty stack and an integer ‘LIMIT’. The size of the stack cannot exceed the ‘LIMIT’.
You are given ‘Q’ queries of the following three types -:
1. PUSH ‘X’ -: Push integer ‘X’ at top of the if its size is less than ‘X’, else do nothing.
2. POP -: Pops and returns the top element of stack or -1 if the stack is empty.
3. INC ‘K’, ‘Y’-: Increments the bottom ‘K’ elements of the stack by ‘Y’. If there are fewer than X elements in the stack, just increment all the elements in the stack.
Your task is to return an array/list, that consists of all elements returned by a query of type ‘POP’ in the same order in which these queries are executed. See the example for more clarity.
Note:1. It is guaranteed that there is at least one query of type ‘POP’.
For Example:
Let there be the following 10 queries and ‘LIMIT’ be 3.
[PUSH 3, PUSH 2, PUSH 1, INC 2 1, PUSH 1, POP, INC 3 3, POP, POP, POP].
Stack after 0th query, i.e PUSH 3, be [3]
Stack after 1st query, i.e PUSH 2, be [3, 2] (top to bottom of the stack is represented by right to the left of list)
Stack after 2nd query, i.e PUSH 1, be [3, 2, 1].
Stack after 3rd query, i.e INC 2 1, be [4, 3, 1]. We increment the bottom 2 elements by 1.
Stack after 4th query, i.e PUSH 1, be [4, 3, 1] as the size of the stack cannot exceed 3.
Stack after 5th query, i.e POP, be [4, 3] and we should return 1 for this query.
Stack after 6th query, i.e INC 3 3, be [7, 6] as stack size is less than 3, so we every element.
Stack after the 7th query, i.e POP, be [7] and we should return 6 for this query.
Stack after 8th query, i.e POP, be [] and we should return 7 for this query.
Stack after the 9th query, i.e POP, be [] and we should return -1 for this query.
Thus we should return an array/list [1, 6, 7, -1].
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case will contain two space-separated integers ‘Q’ and ‘LIMIT’, representing the number of queries, and maximum size of stack respectively.
The next ‘Q’ lines of each test case consist of a string that represents the type of query and then 0 to 2 space-separated integers according to types of query.
Output Format:
For each test case, print space-separated integers returned by a query of type ‘POP’ in the same order in which these queries are executed.
Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= Q <= 10000
1 <= LIMIT <= 10000
1 <= X <= 10000
1 <= K <= 10000
1 <= Y <= 10000
Where ‘T’ is the number of test cases, ‘Q’, ‘LIMIT’, representing the number of queries, and maximum size of stack respectively, and ‘X’, ‘K’, ‘Y’ are integers described in problem statements.
Time limit: 1 sec
2
1 1
POP
10 3
PUSH 3
PUSH 2
PUSH 1
INC 2 1
PUSH 1
POP
INC 3 3
POP
POP
POP
-1
1 6 7 -1
In the first test case, there is only 1 ‘POP’ query, as the stack initially is empty so we should return -1.
For the second test case, refer the problem statement for an explanation.
1
5 5
PUSH 2
PUSH 3
INC 1 1
INC 2 1
POP
4
Do as directed.
The basic idea is to simulate stack using vector/list’. The PUSH and POP query can be performed in O(1) times, and for the query of type INC ‘K’ ‘Y’, we iterate over the first ‘K’ elements (If exist) and increment them by Y, it can take O(K) times.
The steps are as follows:
O(Q*K), where 'Q' is the number of queries and ‘K’ is the maximum number of elements to be incremented in the query of type ‘INC’.
The query of type INC processed in O(K) times and there are ‘Q’ queries, In the worst case most of the queries are of type 3 thus it takes at most O(Q*K) time to process all these queries. Thus, the overall time complexity will be O(Q).
O(Q), where 'Q' is the number of queries
The size of the vector/list ‘STK’ can go up to O(Q), Thus the space complexity will be O(Q).