# Evaluate Reverse Polish Notation (Leetcode #150)

[You are given an array of strings `tokens` that represents an a](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[rithmetic expr](https://leetcode.com/problems/evaluate-reverse-polish-notation/)ession in a [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).

Evaluate the expression. Return *an int*[*eger that represents th*](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[*e value of the*](https://leetcode.com/problems/evaluate-reverse-polish-notation/)*expression*.

**Note** that:

* The valid operators are [`'+'`, `'-'`, `'*'`, and `'/'`.](http://en.wikipedia.org/wiki/Reverse_Polish_notation)
    
* [Each opera](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[nd may be an i](https://leetcode.com/problems/evaluate-reverse-polish-notation/)nteger or anot[her expression.](http://en.wikipedia.org/wiki/Reverse_Polish_notation)
    
* [The](http://en.wikipedia.org/wiki/Reverse_Polish_notation) [division betwe](https://leetcode.com/problems/evaluate-reverse-polish-notation/)en two integers alwa[ys **truncates toward zer**](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[**o**.](https://leetcode.com/problems/evaluate-reverse-polish-notation/)
    
* [There wi](https://leetcode.com/problems/evaluate-reverse-polish-notation/)ll not be any division by zero[.](http://en.wikipedia.org/wiki/Reverse_Polish_notation)
    
* [The input represen](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[ts a valid ari](https://leetcode.com/problems/evaluate-reverse-polish-notation/)thmeti[c expression in a rever](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[se polish nota](https://leetcode.com/problems/evaluate-reverse-polish-notation/)tion.
    
* The answer and all the intermediate ca[lculations can be repre](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[sented in a **32**](https://leetcode.com/problems/evaluate-reverse-polish-notation/)**\-bit** integer.
    

**Example 1:**

```python
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
```

**Example 2:**

```python
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
```

**Example 3:**

```python
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
```

**Constraints:**

* `1 <= tokens.length`[`<= 10<sup>4</sup>`](http://en.wikipedia.org/wiki/Reverse_Polish_notation)
    
* [`tokens[i]` is either an operator:](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[`"+"`,](https://leetcode.com/problems/evaluate-reverse-polish-notation/)[`"-"`, `"*"`, or `"/"`, or an](http://en.wikipedia.org/wiki/Reverse_Polish_notation)[integer in th](https://leetcode.com/problems/evaluate-reverse-polish-notation/)e range `[-200, 200]`.
    

### Answer:

This question required a data structure that support LIFO (Last in first out). This can be seen as an operator will always be between the last 2 numbers.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719331237478/d30025bf-a34d-490a-bb91-f346e01d19b6.png align="center")

If you understand this logic the code is quite simple

```python
class Solution(object):
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        stack = []
        for c in tokens:
            if c == '+':
                stack.append(stack.pop() + stack.pop())
                
            elif c == '-':
                a,b = stack.pop(), stack.pop()
                stack.append(b - a)

            elif c == '*':
                stack.append(stack.pop() * stack.pop())

            elif c == '/':
                a,b = stack.pop(), stack.pop()
                stack.append(int(b / a))
            
            else:
                stack.append(int(c))
        return stack[0]
```

We will keep adding to the stack until we reach an operator which then will perform on the two latest numbers.

**Time Complexity:**

O(N) Since we are only looping through the loop ounce and the popping and operation are all constant time. O(1) \* N = O(N)

**Space Complexity:**  
O(N) We need to create a stack to keep track of the notion which will have max length of N hence this shill give us a space complexity of O(N)
