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
LeetCode 150: Evaluate Reverse Polish Notation with Stack
More Relevant Posts
-
Day 38 of My DSA Journey Today I solved LeetCode 152 – Maximum Product Subarray on LeetCode. 📌 Problem Given an integer array nums, find the contiguous subarray that has the largest product, and return that product. Example: Input: [2,3,-2,4] Output: 6 → subarray [2,3] 🧠 Approach – Dynamic Programming (Tracking Max & Min) This problem is tricky because of negative numbers. Key Idea: • At each index, we maintain: maxProduct → maximum product ending at current index minProduct → minimum product ending at current index 👉 Why min? Because a negative number can turn a small (negative) product into a large positive one. Steps I followed: • Initialize maxProduct, minProduct, and ans with the first element • Traverse the array from left to right • If the current number is negative → swap max and min • Update: maxProduct = max(current, current × maxProduct) minProduct = min(current, current × minProduct) • Update the final answer using maxProduct ⏱ Time Complexity: O(n) — Single pass through the array 📦 Space Complexity: O(1) — No extra space used 💡 Key Learnings ✔ Handling negative numbers in product problems ✔ Using dynamic programming with state tracking ✔ Understanding why tracking both max & min is important This is one of those problems that looks simple but tests deep understanding of edge cases 🚀 Consistency continues — leveling up every day 💪 #100DaysOfCode #DSA #DynamicProgramming #Arrays #LeetCode #Java #ProblemSolving #CodingJourney #DeveloperJourney #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 59 of My DSA Journey Today I solved LeetCode 762 – Prime Number of Set Bits in Binary Representation on . 📌 Problem Given two integers "left" and "right", count how many numbers in this range have a prime number of set bits (1s) in their binary representation. Example: Input: "left = 6, right = 10" Output: "4" --- 🧠 Approach – Bit Manipulation + Prime Check Steps I followed: • For each number in range "[left, right]": - Count number of set bits (1s) using bit manipulation ("n & 1", "n >> 1") • Check if the count is a prime number - Used a predefined list of primes: "{2,3,5,7,11,13,17,19}" • If yes → increment the answer --- ⏱ Time Complexity: "O(n * log n)" Where "n = right - left + 1" (each number takes log n for bit counting) 📦 Space Complexity: "O(1)" — Constant extra space --- 💡 Key Learnings ✔ Practicing Bit Manipulation concepts ✔ Efficiently counting set bits ✔ Combining multiple concepts (bits + prime check) 👉 This problem shows how combining simple ideas can solve interesting problems 🚀 --- Consistency continues — learning something new every day 💪🔥 #100DaysOfCode #DSA #BitManipulation #Math #LeetCode #Java #ProblemSolving #CodingJourney #DeveloperJourney #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
Ever look at a LeetCode problem and think, "Oh, this is just simple math," only to see a constraint of $10^{15}$ staring back at you? That was my experience with the "Count Good Numbers" problem today. The core logic was actually pretty straightforward: even indices get even digits (5 options), and odd indices get prime digits (4 options). But trying to calculate 5^even * 4^odd for massive numbers? Immediate Time Limit Exceeded (TLE). I had to scrap the standard approach and write a custom pow() function using Binary Exponentiation. It’s wild how applying modulo 10^9 + 7 at every single multiplication step within the recursive call is the exact difference between a failing solution and a clean O(log n) pass without integer overflow. Definitely a great reminder that knowing the brute-force math is only half the battle—optimizing it is where the real engineering happens. Back to the grind! #LeetCode #DSA #CPP #CompetitiveProgramming #SoftwareEngineering #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 109 DSA Problem Solving 📌 Problem Solved: Maximum Distance Between a Pair of Values 💡 Level: Medium 🧠 Problem Idea Given two non-increasing arrays, find the maximum distance (j - i) such that: i ≤ j nums1[i] ≤ nums2[j] 🔍 Key Learning Today reinforced a powerful pattern: 👉 When arrays are sorted, always think about Two Pointers before brute force. ⚙️ Approach Used Used Two Pointers (i, j) If condition satisfies → expand j to maximize distance Else → move i to find a smaller value ⏱ Time Complexity O(n + m) ✅ (No nested loops, super efficient) 💭 Real Journey Behind the Solution Initially, I thought about checking all pairs (brute force), but that would lead to TLE. Then I noticed both arrays are non-increasing, which unlocked the two-pointer optimization. This is a reminder that: 👉 Observing constraints carefully can completely change the approach. 📈 Concepts Practiced Two Pointers Greedy Thinking Array Traversal Optimization 🔥 Takeaway Not every problem needs complex logic—sometimes the smartest solution is just about moving pointers wisely. #Day109 #DSAJourney #LeetCode #Coding #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 57 of DSA Journey Solved Next Greater Element II (LeetCode 503) today — a solid problem on Monotonic Stack + Circular Array concepts. 🔍 Key Learnings: • How to use a monotonic decreasing stack efficiently • Handling circular arrays using 2*n traversal • Understanding when and why elements get resolved in stack-based problems 💡 Insight: This problem reinforced that many questions are just variations of the same core pattern. Once you truly understand the pattern, solving similar problems becomes much more intuitive. 📈 Progress > Perfection. Staying consistent and focusing on building strong fundamentals. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/g8Xgd5wx The function goes through each word in the `queries` array. For every query word, it compares it with each word in the `dictionary`. Since all words are assumed to have the same length, it performs a character-by-character comparison. For each pair of words, a counter called `unequal` tracks how many positions have different characters. The code loops through each index of the word and increases this counter whenever the characters at the same position do not match. If the number of differing characters is 2 or less at any point, it means the query word can change into that dictionary word with at most two edits. In this case, the query word is added to the result list, and the inner loop stops early, using `break` to avoid unnecessary comparisons with other dictionary words. Finally, after checking all query words, the function returns the list of valid words that meet the condition. 👉 My Solution: https://lnkd.in/gMnYvSvx If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
Day 87 on LeetCode — Flatten a Multilevel Doubly Linked List 🔗🧠🔥 Now this was a next-level linked list problem — combining recursion with pointer manipulation 💯 🔹 Flatten a Multilevel Doubly Linked List The goal was to convert a multilevel list (with child pointers) into a single-level doubly linked list. 🔹 Approach Used in My Solution (DFS + Recursion) Key idea: • Traverse the list node by node • If a node has a child: – Recursively flatten the child list – Insert the flattened child between current node and next node – Connect prev and next pointers properly – Set child = nullptr • Maintain a last pointer to track the tail of the flattened part This is essentially a DFS traversal on a linked list structure. ⚡ Complexity: • Time Complexity: O(n) • Space Complexity: O(n) (due to recursion stack) 💡 Key Takeaways: • Combined recursion + pointer manipulation effectively • Learned how to flatten hierarchical structures using DFS thinking • Careful handling of next, prev, and child pointers is critical 🔥 This is where linked lists start feeling like real problem-solving, not just traversal. #LeetCode #DSA #Algorithms #DataStructures #LinkedList #DoublyLinkedList #Recursion #DFS #ProblemSolving #Coding #Programming #Cpp #STL #SoftwareEngineering #ComputerScience #CodingPractice #DeveloperLife #TechJourney #CodingDaily #Consistency #100DaysOfCode #BuildInPublic #AlgorithmPractice #CodingSkills #Developers #TechCommunity #SoftwareDeveloper #EngineeringJourney
To view or add a comment, sign in
-
-
🚀 Day 34/100 – DSA Practice (LeetCode) Today I solved “367. Valid Perfect Square” problem. 🔹 Problem Summary: Given a positive integer num, check whether it is a perfect square without using built-in functions like sqrt(). 🔹 What I Learned Today: A perfect square means: → x * x = num where x is an integer Avoiding built-in functions improves problem-solving skills Learned how to think mathematically + logically Practiced loop-based checking approach Understood importance of constraints (large inputs) 🔹 My Approach: Start from i = 1 Calculate i * i If it equals num → return true If it exceeds num → stop and return false ✔ Simple and beginner-friendly approach ✔ No built-in functions used 🔹 Example: Input: 16 → Output: true ✅ Input: 14 → Output: false ❌ 💡 Key Takeaway: Instead of relying on built-in methods, building logic from scratch strengthens problem-solving skills. ⚠️ Missed yesterday’s update due to network issues, but back on track today! #Day34 #100DaysOfCode #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Just crossed 130 LeetCode problems solved! Here are the 5 patterns that unlocked the majority of them: 1️⃣ Sliding Window — O(n) solutions hiding inside O(n²) problems 2️⃣ Two Pointers — eliminate the inner loop, think from both ends 3️⃣ BFS/DFS — once you see graphs everywhere, you can't unsee them 4️⃣ Dynamic Programming — break it into subproblems, trust the recurrence 5️⃣ Binary Search — not just for sorted arrays; think about "search space" Most medium problems are just combinations of these 5. The real milestone isn't the number — it's the moment you stop Googling the approach and start reasoning from first principles. Onto the next 130. 💻 #LeetCode #DataStructures #Algorithms #Java #DSA #Tech
To view or add a comment, sign in
-
-
🚀 Day 89 – DSA Journey | Invert Binary Tree Continuing my daily DSA practice, today I worked on a classic problem that builds strong intuition around tree manipulation. 📌 Problem Practiced: Invert Binary Tree (LeetCode 226) 🔍 Problem Idea: Given a binary tree, invert it by swapping the left and right children of every node. 💡 Key Insight: At each node, simply swap its left and right subtrees. Recursively applying this across the tree results in a complete mirror of the original tree. 📌 Approach Used: • If the node is null → return null • Swap left and right children • Recursively apply the same operation on both subtrees • Return the root after inversion 📌 Concepts Strengthened: • Tree traversal • Recursion • Tree manipulation • Mirror transformation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Simple operations applied recursively can transform complex structures like trees efficiently. On to Day 90! 🚀 #Day89 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development