TLE - you’ve got to reduce the time your program takes to run, so either make it do less work, or make it do the same work but quicker. Here are some ideas for the former.
-
“for i in range(0, no_of_lines): list1.append(str(input()))” There may be 1000000 queries, and you’re reading them one at a time and adding them to a list, only to process them one at a time in the same order you originally read them. This is unnecessary, you can read a query, process it and output any result, then read the next query.
-
“stack = alter + stack” the stack may have 1000000 numbers in it, so I would expect this to be slow. Wouldn’t stack.insert() be quicker?
-
Do you need to convert the input to ints before stacking them? There are no calculations, so it may be quicker to stack them as strings.
Disclaimer: I’m not a python programmer, so I may be talking rubbish.