Evaluate Reverse Polish Notation

Program for Evaluating Reverse Polish Notation Problem

You are provided with an array of strings, tokens, which represents a valid arithmetic expression in Reverse Polish Notation.

Your task is to calculate and return the integer result of evaluating the expression.

  • The operands in the expression can either be integers or the results of previous operations.
  • The supported operators are +, -, *, and /.
  • Assume that any division operation between integers truncates the result toward zero.
Reverse a String

Explanation: ((1 + 2) * 3) – 4 = 5

Constraints:

  • 1 <= tokens.length <= 1000.
  • tokens[i] is “+”, “-“, “*”, or “/”, or a string representing an integer in the range [-100, 100].

Program for Evaluating Reverse Polish Notation Solution

Recommendation for Time and Space Complexity –You should aim for a solution with O(n) time and O(n) space, where n is the size of the input array.

Hints for solving problems

Hint 1 :

A brute force approach would involve repeatedly identifying an operator (+, -, *, /) in the array, computing the result using the operator and its two preceding operands, and replacing these elements in the array. However, this results in an O(n^2) time complexity. Can you think of a more efficient solution? Perhaps a specific data structure could simplify the process.

Hint 2 :

A stack can be used to solve this efficiently. As we traverse the array, we push numbers onto the stack. When an operator is encountered, we pop the top two elements from the stack, perform the operation, and push the result back onto the stack. This ensures the correct order of operations is maintained.

Hint 3 :

Since the array represents a postfix expression, the stack effectively manages the order of operations by always utilizing the most recent operands (those nearest the operator). After completing the iteration, the final result will be the only element left in the stack.

There are mainly 4 approach to solve this problem-

  1. Brute Force Method
  2. Doubly Linked List Method
  3. Recursion Method
  4. Stack Method

1. Brute Force Method

Traverse the array repeatedly to identify operators, calculate results using the operator and its operands, and replace them in the array, resulting in O(n^2) time complexity.

  • Time complexity: O(n^2)
  • Space complexity: O(n)

Code

2. Doubly Linked List Method

Use a doubly linked list to store tokens, enabling efficient insertion and deletion of elements while calculating results for operators encountered during traversal.

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

Code

3. Recursion Method

Process the tokens recursively, where each operator triggers recursive calls to evaluate its operands, working backward through the array to maintain the correct order.

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

Code

4. Stack Method

Iterate through the tokens, pushing numbers onto the stack. When encountering an operator, pop two numbers, compute the result, and push it back onto the stack, ensuring O(n) time complexity.

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

Code

More Articles