Tags: Stack, Easy, O(n)
At first glance, there are two types of characters in the tokens: either numbers or operation. We need to treat them differently.
To efficiently calculate we can use a Stack to store the numbers, and everytime we encounter an operation symbol, we pop back the last two numbers and add the result back.
- Initialize stack
- Loop through
tokens
, if character is number, add to stack, and if it's not, pop the last two numbers from stack, calculate the result, and add back to stack
- Time complexity:
$$O(n)$$ Loop throughtokens
which hasn
elements. - Space complexity:
$$O(n)$$ At most the stack is filled withn
numbers.
def rev_polish(t):
s = []
for x in t:
if x not in "+-*/":
s.append(int(x))
else:
p = s.pop()
q = s.pop()
if x == '+':
s.append(q+p)
if x == '*':
s.append(q*p)
if x == '-':
s.append(q-p)
if x == '/':
s.append(int(q/p))
return s.pop()
- When you are familiar with Stack, this problem becomes trivial / easy.
- Need to be careful of the order of operation.
a-b != b-a
, and stack behaviour is last in first out. - Also need to be careful of decimal truncation.
In python
//
is floor, and for operations involving negative numbers it can give wrong results.