Tensorum
YouTubeশেখা শুরু করুন
LeetCode 150 · Expression Evaluation

Evaluate Reverse Polish Notation

LeetCode 150 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Operands on a stack; operator pops two.

মাঝারিArrayMathStack
LeetCode-এ খুলুন ↗

আমি Evaluate Reverse Polish Notation (LeetCode 150) এভাবে সমাধান করেছি — Expression Evaluation প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

In RPN every operator applies to the two most recent values, so push numbers on a stack and, at an operator, pop two, combine, and push the result.

Approach

  1. 1

    Number → push.

  2. 2

    Operator → b = pop, a = pop, push a op b.

  3. 3

    Division truncates toward zero: int(a / b).

আমি কীভাবে সমাধান করেছি

Stack evaluationPythonO(n) time
class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        #store the operation with operator as key in a map
        operations = {
            "*": lambda a,b:a*b,
            "+": lambda a,b:a+b,
            "-": lambda a,b:a-b,
            "/": lambda a,b:int(a/b)
        }
        stack = []
        for token in tokens:
            if token in operations:
                b = stack.pop()
                a = stack.pop()
                operation = operations[token]
                cur_val = operation(a,b)
                stack.append(cur_val)
            else:
                stack.append(int(token))
        return stack.pop()
স্টেপ-থ্রু লোড হচ্ছে…

Evaluate Reverse Polish Notation — সাধারণ প্রশ্ন

Evaluate Reverse Polish Notation-এর পেছনে intuition কী?

In RPN every operator applies to the two most recent values, so push numbers on a stack and, at an operator, pop two, combine, and push the result.

Evaluate Reverse Polish Notation (LeetCode 150) কীভাবে সমাধান করবেন?

1. Number → push. 2. Operator → b = pop, a = pop, push a op b. 3. Division truncates toward zero: int(a / b).

Evaluate Reverse Polish Notation-এর time আর space complexity কত?

Time: O(n)। Space: O(n)। (Accepted approach: Stack evaluation।)

Evaluate Reverse Polish Notation-এ সাধারণ ভুলগুলো কী?

Pop order matters for − and /. Python's // floors negatives; int(a / b) truncates as required.