150. Evaluate Reverse Polish Notation LeetCode Solution

In this guide, you will get 150. Evaluate Reverse Polish Notation LeetCode Solution with the best time and space complexity. The solution to Evaluate Reverse Polish Notation problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Evaluate Reverse Polish Notation solution in C++
  4. Evaluate Reverse Polish Notation solution in Java
  5. Evaluate Reverse Polish Notation solution in Python
  6. Additional Resources
150. Evaluate Reverse Polish Notation LeetCode Solution image

Problem Statement of Evaluate Reverse Polish Notation

You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:

The valid operators are ‘+’, ‘-‘, ‘*’, and ‘/’.
Each operand may be an integer or another expression.
The division between two integers always truncates toward zero.
There will not be any division by zero.
The input represents a valid arithmetic expression in a reverse polish notation.
The answer and all the intermediate calculations can be represented in a 32-bit integer.

Example 1:

Input: tokens = [“2″,”1″,”+”,”3″,”*”]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: tokens = [“4″,”13″,”5″,”/”,”+”]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

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 <= 104
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].

See also  1047. Remove All Adjacent Duplicates In String LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

150. Evaluate Reverse Polish Notation LeetCode Solution in C++

class Solution {
 public:
  int evalRPN(vector<string>& tokens) {
    stack<long> stack;
    const unordered_map<string, function<long(long, long)>> op{
        {"+", std::plus<long>()},
        {"-", std::minus<long>()},
        {"*", std::multiplies<long>()},
        {"/", std::divides<long>()}};

    for (const string& token : tokens)
      if (op.contains(token)) {
        const long b = stack.top();
        stack.pop();
        const long a = stack.top();
        stack.pop();
        stack.push(op.at(token)(a, b));
      } else {
        stack.push(stoi(token));
      }

    return stack.top();
  }
};
/* code provided by PROGIEZ */

150. Evaluate Reverse Polish Notation LeetCode Solution in Java

class Solution {
  public int evalRPN(String[] tokens) {
    final Map<String, BinaryOperator<Long>> op = Map.of(
        "+", (a, b) -> a + b, "-", (a, b) -> a - b, "*", (a, b) -> a * b, "/", (a, b) -> a / b);
    Deque<Long> stack = new ArrayDeque<>();

    for (final String token : tokens)
      if (op.containsKey(token)) {
        final long b = stack.pop();
        final long a = stack.pop();
        stack.push(op.get(token).apply(a, b));
      } else {
        stack.push(Long.parseLong(token));
      }

    return stack.pop().intValue();
  }
}
// code provided by PROGIEZ

150. Evaluate Reverse Polish Notation LeetCode Solution in Python

class Solution:
  def evalRPN(self, tokens: list[str]) -> int:
    stack = []
    op = {
        '+': lambda a, b: a + b,
        '-': lambda a, b: a - b,
        '*': lambda a, b: a * b,
        '/': lambda a, b: int(a / b),
    }

    for token in tokens:
      if token in op:
        b = stack.pop()
        a = stack.pop()
        stack.append(op[token](a, b))
      else:
        stack.append(int(token))

    return stack.pop()
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.