Evaluate Reverse Polish Notation

Medium

Evaluate Reverse Polish Notation

Problem Statement

Given the Reverse Polish Notation (RPN) of an arithmetic expression as an array of strings, evaluate the expression.

Reverse Polish Notation is a mathematical notation where operators follow their operands. The expression is evaluated by applying the operators from left to right.

Input Constraints

  • The input
    expressions
    will be an array of strings
    nums
    and
    operations
    , representing the arithmetic expression in Reverse Polish Notation.
  • The input
    expressions
    will only contain the four basic arithmetic operators:
    +
    ,
    -
    ,
    *
    ,
    /
    .
  • The input
    expressions
    will not contain any invalid characters or invalid operations.
  • The input
    expressions
    will only contain positive integers as operands.
  • The input
    expressions
    will always form a valid arithmetic expression.

Output Constraints

  • The function should return the result of the arithmetic expression as a floating-point number.
  • The result should be accurate up to four decimal places.

Time and Space Complexity

  • The function should have a time complexity of O(n), where n is the number of operations in the input array.
  • The function should have a space complexity of O(n), where n is the number of operations in the input array.

Assumptions

  • The input
    expressions
    will always contain a valid stack-based RPN expression.
  • The input
    expressions
    will never contain any invalid characters or invalid operations.

Example

Input: {"tokens":["2","1","+","3","*"]} Output: 9

CompaniesAmazon
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.