LeetCode 150: Evaluate Reverse Polish Notation with Stack

Day 36 of My DSA Journey Today I solved LeetCode 150 – Evaluate Reverse Polish Notation (RPN) on LeetCode. 📌 Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are: +, -, *, / Each operand may be an integer. Example: Input: ["2","1","+","3","*"] Output: 9 Explanation: (2 + 1) * 3 = 9 🧠 Approach – Stack This problem is a perfect application of the Stack data structure. Steps I followed: • Traverse the tokens array. • If the element is a number → push it onto the stack. • If it is an operator: Pop the top two elements (a and b) Perform the operation (a operator b) Push the result back into the stack • At the end, the stack contains the final result. ⏱ Time Complexity: O(n) — We process each token once 📦 Space Complexity: O(n) — Stack to store operands 💡 Key Learnings ✔ Understanding Reverse Polish Notation (Postfix Expression) ✔ Applying stack for expression evaluation ✔ Handling operator precedence implicitly using stack This problem connects DSA with compiler/interpreter concepts, which makes it even more interesting 🚀 Consistency continues — improving every day 💪 #100DaysOfCode #DSA #Stack #LeetCode #Java #ProblemSolving #CodingJourney #DeveloperJourney #Programming #SoftwareEngineering

  • text

To view or add a comment, sign in

Explore content categories