𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 — 𝗪𝗵𝗲𝗻 𝗦𝘁𝗮𝗰𝗸𝘀 𝗚𝗲𝘁 𝗚𝗿𝗲𝗲𝗱𝘆 Yesterday: Valid Parentheses. Basic stack. Today: Remove Duplicate Letters. Stack + greedy + frequency tracking. Level up. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟯𝟭𝟲: Remove Duplicate Letters (Medium) 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: Remove duplicate letters so the result is: Smallest in lexicographical order (dictionary order) Contains each letter exactly once Example: "bcabc" → "abc" The trick? Knowing when to remove a character from the stack to make room for a better one. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Monotonic stack + greedy decisions. 👉 Track frequency: how many times each character appears ahead 👉 Track visited: have we already used this character? 👉 For each character, pop stack if: Stack top is larger (worse for lexicographical order) Stack top appears again later (we can use it then) 👉 Push current character and mark visited Time: O(n), Space: O(1) — only 26 letters max. 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: This combines three patterns: Monotonic stack (maintaining order) Greedy algorithm (making locally optimal choices) Frequency tracking (knowing what's ahead) Solving it wasn't about knowing one technique. It was about combining them. 𝗖𝗼𝗱𝗲: https://lnkd.in/gzw6ACFr 57 down. 43 to go. 𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination
More Relevant Posts
-
𝗗𝗮𝘆 𝟲𝟲/𝟭𝟬𝟬 — 𝗘𝘃𝗮𝗹𝘂𝗮𝘁𝗲 𝗥𝗲𝘃𝗲𝗿𝘀𝗲 𝗣𝗼𝗹𝗶𝘀𝗵 𝗡𝗼𝘁𝗮𝘁𝗶𝗼𝗻 Day 66. Two-thirds done. Today's problem? The reason calculators exist. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟭𝟱𝟬: Evaluate Reverse Polish Notation (Medium) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Evaluate expressions in Reverse Polish Notation (RPN). Numbers come first, operators after. Example: ["2","1","+","3","*"] → ((2+1)*3) = 9 No parentheses needed. No order of operations confusion. Just read left to right. 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Stack. When you see a number, push it. When you see an operator, pop two numbers, apply the operation, push the result. The last number standing? That's your answer. Used ArrayList as a stack for cleaner syntax with removeLast(). Same LIFO behavior. 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This is how calculators and compilers evaluate expressions internally. RPN eliminates ambiguity. "3 + 4 * 5" needs rules. "3 4 5 * +" doesn't. Understanding this makes you understand how expression parsing works. 𝗖𝗼𝗱𝗲: https://lnkd.in/gXwcqVdP 𝗗𝗮𝘆 𝟲𝟲/𝟭𝟬𝟬 ✅ 𝟲𝟲 𝗱𝗼𝘄𝗻. 𝟯𝟰 𝘁𝗼 𝗴𝗼. #100DaysOfCode #LeetCode #Stack #RPN #Algorithms #ExpressionEvaluation #CodingInterview #Programming #Java #MediumLevel #CompilerDesign
To view or add a comment, sign in
-
Day 74/365 – Remove Duplicate Letters Problem: Given a string s, remove duplicate letters so that each letter appears once and the result is the smallest lexicographical order possible. Example: "bcabc" → "abc" "cbacdcbc" → "acdb" 💡 Key Idea Use a Monotonic Stack to maintain characters in lexicographically smallest order. We also track: • Last occurrence of each character • Whether a character is already used in the result 🧠 Approach 1️⃣ Record the last index of each character. 2️⃣ Traverse the string. 3️⃣ Skip characters already included. 4️⃣ While stack top is bigger than current character and it appears later again, remove it to get a smaller result. 5️⃣ Push current character into the stack. 📦 Key Logic while(!stack.isEmpty() && stack.peek() > c && lastIndex[stack.peek() - 'a'] > i) { visited[stack.pop() - 'a'] = false; } This ensures: Lexicographically smallest result Each character appears only once 📊 Complexity ⏱ Time: O(n) 📦 Space: O(1) (since only 26 letters) ✨ Key Learning This is a classic Monotonic Stack + Greedy problem. Pattern to remember: 👉 Remove previous larger elements if they appear again later. This pattern appears in problems like: Smallest Subsequence Remove K Digits Monotonic stack optimization problems #Day74 #365DaysOfCode #LeetCode #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #Java #DSA #CodingInterview
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge Today’s problem was Find Unique Binary String. Problem: We are given n unique binary strings of length n. The task is to return any binary string of length n that does not exist in the given array. At first glance, brute force might come to mind: Generate all 2^n binary strings and check which one is missing. But that’s inefficient. 💡 Better Idea – Diagonal Trick (Inspired by Cantor’s Diagonal Argument) Instead of checking all possibilities, we construct a new string by flipping the i-th bit of the i-th string. The new string will differ from every string in the array at least at one position. Therefore, it cannot match any existing string. ⚡ Time Complexity: O(n) ⚡Space Complexity: O(n) 🔍 Key Insight: By flipping the diagonal bits, we guarantee the generated binary string is different from every given string. Problems like this remind me that a simple mathematical idea can lead to a really elegant algorithm. #LeetCode #DailyCodingChallenge #Java #Algorithms #DataStructures #ProblemSolving #CodingInterview
To view or add a comment, sign in
-
-
🚀 A quick tip from the “Rotate String” problem! A popular trick to check if one string is a rotation of another is this: return s.length() == goal.length() && (s + s).contains(goal); The idea is pretty straightforward: if goal is a rotation of s, it’ll show up somewhere inside s + s. For example: s = "abcde" goal = "cdeab" s + s = "abcdeabcde" and you can see "cdeab" right there. But here’s the catch: the .contains() method does a substring search that can be slow in the worst case (up to O(n²)). So while this trick usually works fine, it’s not the most efficient for all cases. But to reduce the time complexity, we can use KMP (Knuth–Morris–Pratt) algorithm instead: Build the LPS (Longest Prefix Suffix) array for goal Use KMP to search for goal inside s + s This guarantees the search will run in O(n) time. 💡 It’s a good reminder that a simple trick can solve a problem quickly, but knowing classic algorithms like KMP helps to build solutions that perform well even in tricky situations. #Algorithms #Coding #TechTips #Java #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟴/𝟭𝟬𝟬 — 𝗦𝗶𝗺𝘂𝗹𝗮𝘁𝗶𝗻𝗴 𝗦𝘁𝗮𝗰𝗸 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 Day 58. Back to a problem I solved on Day 22. Except Day 22 me didn't really understand it. Day 58 me does. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟭𝟰𝟰𝟭: Build an Array With Stack Operations (Medium) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a target array and stream [1,2,3...n], return the sequence of "Push" and "Pop" operations to build the target. Example: Target: [1,3] Stream: [1,2,3] Operations: ["Push", "Push", "Pop", "Push"] (Push 1, push 2, pop 2, push 3 → Result: [1,3]) 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: You don't actually need a stack. You need to simulate stack behavior. Iterate from 1 to the last target value: If current number is in target → "Push" If not → "Push" then "Pop" (skip it) That's it. Simple simulation. 𝗪𝗵𝗮𝘁'𝘀 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝗡𝗼𝘄: Day 22: I solved it but didn't really grasp why we simulate instead of using a real stack. Day 58: I see it—we're not solving a stack problem. We're solving a sequencing problem. The "stack" is just the metaphor. 36 days of practice. Same problem. Deeper understanding. 𝗖𝗼𝗱𝗲: https://lnkd.in/gHhnp_TX 58 down. 42 to go. 𝗗𝗮𝘆 𝟱𝟴/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #Simulation #Algorithms #ProblemSolving #CodingInterview #Programming #Java #DeepLearning #GrowthMindset
To view or add a comment, sign in
-
Everyone learns stacks. But very few understand where they actually matter. Take a simple problem: Checking if brackets are balanced. Most people think it’s about counting. It’s not. It’s about order. Here’s what really happens behind the scenes: → You scan the expression left to right → Every opening bracket goes into a stack → Every closing bracket tries to match the last opening one If it matches → remove it If it doesn’t → the entire structure breaks That’s the moment you realize: Stacks aren’t just data structures. They are decision systems. They enforce rules like: Last In → First Out And that’s exactly how: • Code editors validate syntax • Compilers detect errors • Browsers manage navigation history A simple example: [(a+b)] → Valid ✔ [(a+b] → Invalid ❌ Same characters. Different structure. That’s the difference between working code and broken logic. The lesson? In programming — and in systems — structure beats quantity. Always. #DataStructures #Python #ProblemSolving #CodingJourney #AIThinking
To view or add a comment, sign in
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 𝐒𝐨𝐥𝐯𝐞𝐝 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: Count Submatrices With Equal Frequency of X and Y (Medium) Today’s problem looked simple at first glance, but had a subtle twist, we needed to count submatrices that: ✔️ 𝐈𝐧𝐜𝐥𝐮𝐝𝐞 𝐭𝐡𝐞 𝐭𝐨𝐩-𝐥𝐞𝐟𝐭 𝐜𝐞𝐥𝐥 (𝟎,𝟎) ✔️ 𝐇𝐚𝐯𝐞 𝐞𝐪𝐮𝐚𝐥 𝐧𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 '𝐗' 𝐚𝐧𝐝 '𝐘' ✔️ 𝐂𝐨𝐧𝐭𝐚𝐢𝐧 𝐚𝐭 𝐥𝐞𝐚𝐬𝐭 𝐨𝐧𝐞 '𝐗' 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Instead of checking every possible submatrix (which would be too slow), we can use 𝟐𝐃 𝐩𝐫𝐞𝐟𝐢𝐱 𝐬𝐮𝐦𝐬 to efficiently track counts of 'X' and 'Y'. 🔹 𝐁𝐮𝐢𝐥𝐝 𝐭𝐰𝐨 𝐩𝐫𝐞𝐟𝐢𝐱 𝐦𝐚𝐭𝐫𝐢𝐜𝐞𝐬: px[i][j] → count of 'X' from (0,0) to (i,j) py[i][j] → count of 'Y' from (0,0) to (i,j) 🔹 𝐓𝐡𝐞𝐧 𝐟𝐨𝐫 𝐞𝐚𝐜𝐡 𝐜𝐞𝐥𝐥 (𝐢,𝐣): If px[i][j] == py[i][j] and px[i][j] > 0, we found a valid submatrix! 𝐖𝐡𝐲 𝐭𝐡𝐢𝐬 𝐰𝐨𝐫𝐤𝐬: Every (i,j) represents a submatrix from (0,0) → (i,j). Prefix sums let us compute counts in O(1) per cell → overall O(m × n). 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: Time: O(m × n) Space: O(m × n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Prefix sums are incredibly powerful for grid problems especially when dealing with frequency/count constraints. Another day, another problem solved. Let’s keep building momentum! #LeetCode #DailyCoding #DataStructures #Algorithms #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟲𝟵/𝟭𝟬𝟬 — 𝗥𝗲𝗺𝗼𝘃𝗲 𝗞 𝗗𝗶𝗴𝗶𝘁𝘀 Day 69. One day from 70. This problem broke my brain. Then it clicked. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟰𝟬𝟮: Remove K Digits (Medium) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a number as a string and k, remove k digits to make the smallest possible number. Example: "1432219", k=3 → "1219" Which 3 digits do you remove? And in what order? 𝗧𝗵𝗲 𝗜𝗻𝘀𝗶𝗴𝗵𝘁: Greedy + Monotonic Stack. Remove a digit if the next digit is smaller. Keep the stack increasing. This builds the smallest number left-to-right. Example: "1432219" See '4' then '3'? Remove '4' See '3' then '2'? Remove '3' See '2' then '2'? Keep both Result: "12219" → remove trailing '9' → "1219" 𝗠𝘆 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: StringBuilder as stack. For each digit: While stack top > current digit AND k > 0: pop and decrement k Append current digit After loop, remove remaining k digits from the end. Strip leading zeros. Time: O(n), Space: O(n) 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This combines greedy thinking with monotonic stack. Two patterns, one solution. Understanding when to be greedy and when to use a stack—that's the skill. 𝗖𝗼𝗱𝗲: https://lnkd.in/gv36YUfj 𝗗𝗮𝘆 𝟲𝟵/𝟭𝟬𝟬 ✅ 𝟲𝟵 𝗱𝗼𝘄𝗻. 𝟯𝟭 𝘁𝗼 𝗴𝗼. 𝗧𝗼𝗺𝗼𝗿𝗿𝗼𝘄 = 𝟳𝟬. #100DaysOfCode #LeetCode #MonotonicStack #GreedyAlgorithm #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination #Day70Tomorrow
To view or add a comment, sign in
-
Day 72: Binary Search Squared 🏔️ Problem 3296: Minimum Number of Seconds to Make Mountain Height Zero Today’s problem was a masterclass in optimization. The goal: reduce a mountain's height to zero using workers who take progressively more time for each unit of height they remove. The Strategy: • Binary Search on Answer: Since the time needed is monotonic, I used binary search to find the minimum seconds required to finish the job. • Nested Binary Search: Inside that loop, I ran a second binary search for each worker to calculate the maximum height they could handle within that time limit. • Efficiency: This "Double Binary Search" approach kept the runtime lean even with a massive search space up to 10^16. Solving a "Binary Search inside a Binary Search" problem is a great way to test your grip on time complexity. It’s all about finding that optimal boundary. 🚀 #LeetCode #Java #BinarySearch #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
✳️Day 23 of #100DaysOfCode✳️ 🚀Solved Remove Duplicate Letters ✅The goal is to remove duplicate letters so that every letter appears once, ensuring the result is the smallest in lexicographical order among all possible results. 🧠 My Approach & Implementation Steps: To solve this efficiently in O(n) time, I used a Greedy approach supported by a Monotonic Stack: 1️⃣Frequency Map: First, I built a frequency array freq[26] to keep track of the remaining count of each character in the string. This tells the algorithm if a character can be safely removed and re-added later. 2️⃣Visited Tracking: I used a boolean array visited[26] to ensure each character is added to our result only once, maintaining the "unique" constraint. 3️⃣Monotonic Stack Logic: As I iterated through the string: I decremented the character's frequency. If the character was already in the stack, I skipped it. 4️⃣The Crucial Part: While the current character was smaller than the top of the stack AND that top character appeared again later in the string (checked via the frequency map), I popped the stack and marked that character as "not visited." 💯Result Construction: Finally, I pushed the current character onto the stack and built the final string using a StringBuilder. 📊 Results: Runtime: 2 ms (Beats 89.98%) Memory: 43.64 MB (Beats 78.91%) ⚡This problem is a great reminder of how powerful stacks can be when you need to maintain a specific order while processing linear data. Onward to the next challenge! 💻🔥 #LeetCode #Java #DataStructures #Algorithms #CodingLife #ProblemSolving #SoftwareEngineering Anchal Sharma Ikshit ..
To view or add a comment, sign in
-
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