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
Abhishek Agrahari’s Post
More Relevant Posts
-
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
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gZ52pSZR 💡 My thought process: The solution operates in two separate passes: 1. Left-to-Right Pass: For each element at index "i", calculate the distance to all previous occurrences of that value. If a value has appeared "k" times before, the sum of distances is found using this formula: (count * current_index) - (sum of previous_indices). By storing the count and the running sum of indices in a hash map, we can compute this in constant time. 2. Right-to-Left Pass: The same logic is applied in reverse to calculate the distance from the current index to all future occurrences. In this case, the formula changes to: (sum of future_indices) - (count * current_index). By adding the results from both directions, the code captures the total absolute difference for every identical pair without the performance cost of a nested loop. 👉 My Solution: https://lnkd.in/gfEc7Kis If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering
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 48/100 – LeetCode DSA Practice ✅ Problem Solved: 169. Majority Element Today’s problem was about finding the element that appears more than ⌊n/2⌋ times in an array. The question guarantees that such an element always exists. 💡 My Approach (Brute Force): I used two nested loops For each element, I counted how many times it appears in the array If the count is greater than n/2, I returned that element 🔍 Logic in Simple Words: Pick one element Compare it with all elements Count matches If count > n/2 → that’s the majority element 🚀 What I Learned Today: Importance of understanding the definition of majority element How brute force helps build logic step-by-step Learned about better optimization using Boyer-Moore Voting Algorithm (O(n), O(1)) Always try to improve from correct → optimal solution 📌 Problem Summary: Find the element that appears more than half the size of the array. Since it’s guaranteed to exist, we don’t need to worry about edge cases of absence. ✨ Next Step: Practice optimized approaches and improve problem-solving speed! #Day48 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #ProblemSolving #LearnToCode #TechSkills #Programmer #CodingLife #Consistency #GrowthMindset
To view or add a comment, sign in
-
-
Day 28 of #100DaysOfCode 💻 Today’s focus: Subsets II (LeetCode 90) Building on yesterday’s Subset Sum (Problem 1), today I explored how the same recursion pattern works when duplicates are involved. 📌 What I focused on: At each step → either pick the element or skip it (same as Subset 1) Sorting the array to handle duplicates Skipping repeated elements at the same recursion level 💡 Realization: Subset II is not a completely new problem—it’s an extension of Subset 1 with an extra constraint. Understanding the base pattern made it much easier to adapt and solve this one. Felt good to see how concepts connect step by step. Still getting more comfortable with recursion and backtracking 🚀 #LeetCode #DSA #Recursion #Backtracking #CodingJourney
To view or add a comment, sign in
-
-
Day 6 🚀 Solved: Maximum Depth of Binary Tree (LeetCode 104) This is a classic recursion problem on trees. 💡 Key idea: The depth of a tree is 1 + the maximum depth of its left and right subtrees. 🔹 Approach: If the node is null → depth is 0 Recursively calculate depth of left and right Take the maximum and add 1 ⏱️ Time Complexity: O(n) 🔗 GitHub: https://lnkd.in/gz5mBpDx #DSA #LeetCode #Coding
To view or add a comment, sign in
-
Day 32/50 – LeetCode Challenge 🧩 Problem #416 – Partition Equal Subset Sum Today’s problem focused on determining whether an array can be divided into two subsets with equal sum, which is a classic Dynamic Programming (Subset Sum) problem. 📌 Problem Summary: Given an array of integers, check if it can be partitioned into two subsets such that their sums are equal. 🔍 Approach Used ✔ First calculated the total sum of the array ✔ If the sum is odd → not possible to divide equally ✔ Converted the problem into: Can we find a subset with sum = total / 2 ? ✔ Used a set (DP approach) to store all possible sums ✔ Iteratively built new sums by adding each number ✔ Checked if the target sum exists ⏱ Time Complexity: O(n × target) 📦 Space Complexity: O(target) 💡 Key Learning ✔ Converting problems into Subset Sum pattern ✔ Using Dynamic Programming with sets ✔ Thinking in terms of possibility instead of brute force ✔ Recognizing important DP patterns This problem helped me understand how to simplify complex problems into familiar patterns. Consistency builds strong problem-solving intuition 🚀 🔗 Problem Link: https://lnkd.in/gCRhSyaz #50DaysOfLeetCode #LeetCode #DSA #DynamicProgramming #SubsetSum #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
A Lesson in Parity and Circular DP 🚀 After a long break from the LeetCode contest circuit, I jumped back in for Weekly Contest 496. I managed to solve 4/4, AK!. But it wasn't a perfect run—I had 3 Wrong Answer (WA) penalties along the way! The "contest rust" was real, but those mistakes forced me to dig deeper into my Dynamic Programming fundamentals. Here are the exact techniques I used to solve the hardest problems of the set (Q3 and Q4): 💡 Technique 1: Prefix/Suffix Management for "Skip" Logic (Question 3) Q3 required us to find the minimum operations to make an array satisfy maximum peak conditions. The catch? Managing the transitions, especially for even-sized arrays where you are forced to "skip" certain elements to maintain parity. The Approach: Instead of recalculating the cost every time, I used Prefix and Suffix states. By pre-calculating the cumulative cost of forming peaks from the left (prefix) and from the right (suffix), you can efficiently calculate the total cost of forcing a "skip" at any arbitrary index i in O(1) time per index. 💡 Technique 2: Breaking Circular Dependencies (Question 4) Q4 was a brilliant extension of Q3, but the array was now circular. The Approach: I used Recursive DP with Memoization, utilizing a classic trick of breaking the circle. You cannot evaluate a circular array in one clean sweep. Instead, you branch it into two Pass scenarios: Pass 1: Assume the 0th index IS utilized. This means you must explicitly block the (n-1)th index from being used to prevent a circular clash. Pass 2: Assume the 0th index is NOT utilized. Now, the (n-1)th index is free to be evaluated normally. By taking the minimum of minOps(Pass 1) and minOps(Pass 2), you safely cover all valid combinations. #LeetCode #DynamicProgramming #Algorithms #TechInterviews #SeniorSoftwareEngineer #SDE
To view or add a comment, sign in
-
-
LeetCode Progress 32/50 — Generate Parentheses Today I worked on a problem focused on recursion and backtracking 🧩 💡 The challenge was to generate all possible combinations of well-formed parentheses for a given number of pairs. 🧠 Approach: Used a backtracking approach by recursively adding opening and closing parentheses while maintaining valid combinations at each step. This ensured only well-formed patterns were generated. ⏱ Time Complexity: O(4ⁿ / √n) 💡 What I learned: This problem highlighted how backtracking can be used to systematically explore valid combinations while pruning invalid paths early. 📈 Strengthening understanding of recursion and improving confidence in solving pattern-generation problems. #LeetCode #DSA #Backtracking #ProblemSolving #Cpp #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
🚀 Day 69 of #100DaysOfCode 🔥 Problem: Majority Element II (LeetCode 229) Today’s challenge was all about finding elements that appear more than ⌊n/3⌋ times in an array. Sounds simple, but the twist is doing it efficiently! 💡 Key Insight: Instead of counting frequencies using extra space, we can use an optimized approach based on the Boyer-Moore Voting Algorithm. ✨ Approach Highlights: • There can be at most 2 majority elements (> n/3) • Maintain 2 candidates and their counts • First pass → find potential candidates • Second pass → verify their frequency ⚡ Why this works? Because any element appearing more than n/3 times will survive the elimination process. 📈 Complexity: • Time: O(n) • Space: O(1) 🎯 Takeaway: Smart algorithms > brute force. Understanding patterns like voting algorithms can save both time and space! #DSA #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode #Algorithms #Programming
To view or add a comment, sign in
-
More from this author
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
Insightful!