O29

Evaluate Reverse Polish Notation

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

Evaluate the value of an arithmetic expression in Reverse Polish Notation (postfix notation). Valid operators are '+', '-', '*', and '/'.

Function Signature (Python)

eval_rpn(expression: list[str]) -> int

Input & Output Contract

Output Rule:Hesaplanan tam sayı sonucu.

System Constraints

1 <= ifade.length <= 10^4
ifade[i] bir operatör veya [-200, 200] aralığında bir tamsayıdır
Sıfıra bölme hatası oluşturmayacak geçerli ifadeler verilir

Example Execution Walkthrough

ifade = ['2', '1', '+', '3', '*'] -> ((2 + 1) * 3) = 9
ifade = ['4', '13', '5', '/', '+'] -> (4 + (13 / 5)) = 4 + 2 = 6

['-7', '3', '/'] -> int(-7 / 3) = -2.  -7 // 3 = -3 olduğu için taban bölmesi yanlıştır.
Örnek 3'teki 6 / -132 adımı da aynı tuzaktır: sıfıra doğru 0'dır, // ile -1 olur ve sonuç 22 yerine 12 çıkar.

Example Test Scenarios (4)

Example 1
Input:[["2","1","+","3","*"]]
Expected Output:9
Example 2
Input:[["4","13","5","/","+"]]
Expected Output:6
Example 3
Input:[["10","6","9","3","+","-11","*","/","*","17","+","5","+"]]
Expected Output:22
Example 4: Negatif Bölme (// tuzağı)
Input:[["-7","3","/"]]
Expected Output:-2
Loading Python Editor...
Input Parameters
[
  [
    "2",
    "1",
    "+",
    "3",
    "*"
  ]
]
Expected Output
9