𝗗𝗮𝘆 𝟱𝟴/𝟭𝟬𝟬 — 𝗦𝗶𝗺𝘂𝗹𝗮𝘁𝗶𝗻𝗴 𝗦𝘁𝗮𝗰𝗸 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 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
More Relevant Posts
-
𝗗𝗮𝘆 𝟲𝟴/𝟭𝟬𝟬 — 𝗧𝗶𝗺𝗲 𝗡𝗲𝗲𝗱𝗲𝗱 𝘁𝗼 𝗕𝘂𝘆 𝗧𝗶𝗰𝗸𝗲𝘁𝘀 🎟️ Day 68. Sometimes you don't need to simulate. You just need to think. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟮𝟬𝟳𝟯: Time Needed to Buy Tickets (Easy) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: People in a queue buying tickets. Each person needs a certain number of tickets. They buy one at a time and go to the back of the line if they need more. How long until person k finishes buying? 𝗧𝗵𝗲 𝗜𝗻𝘀𝗶𝗴𝗵𝘁: You could simulate the queue. Or you could realize: People before position k contribute min(tickets[i], target) People after position k contribute min(tickets[i], target - 1) Why target - 1 for people after? Because person k finishes before they get another turn. No simulation needed. Just math. 𝗠𝘆 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: One loop. For each person: If before or at position k: add min(tickets[i], tickets[k]) If after position k: add min(tickets[i], tickets[k] - 1) Sum it up. Done. Time: O(n), Space: O(1) 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: The best solutions eliminate unnecessary work. Simulation is O(n × max(tickets)). Math is O(n). 𝗖𝗼𝗱𝗲: https://lnkd.in/gn58Hcf7 𝗗𝗮𝘆 𝟲𝟴/𝟭𝟬𝟬 ✅ 𝟲𝟴 𝗱𝗼𝘄𝗻. 𝟯𝟮 𝘁𝗼 𝗴𝗼. #100DaysOfCode #LeetCode #Queue #Algorithms #MathematicalThinking #CodingInterview #Programming #Java #Optimization #LogicalThinking
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 10/100 – First Missing Positive | LeetCode (Hard) Double digits in my #100DaysOfChallenges journey! Today’s challenge was a classic interview problem that tests array manipulation + in-place hashing logic. 🧩 Problem: Given an unsorted integer array, return the smallest missing positive integer. Constraints: Must run in O(n) time Use O(1) extra space 🧠 Approach Used: Cyclic Sort (Index Placement Technique) Instead of using extra space like HashSet, I used an in-place strategy: ✔️ Place each number x at index x - 1 ✔️ Ignore negative numbers and numbers > n ✔️ Swap until every valid number is in its correct position ✔️ Scan the array to find the first index where nums[i] != i + 1 Core Idea: If 1 is at index 0, 2 at index 1, etc., The first mismatch gives the missing positive. 💡 Why This Is Smart: It uses the array itself as a hash structure No extra memory required Maintains linear time complexity ⏱ Complexity: Time: O(n) Space: O(1) 🎯 Key Learning: In-place hashing is powerful Hard problems often combine simple ideas cleverly Edge cases (negatives, duplicates, large values) matter a lot This problem strengthened my: 👉 Index mapping intuition 👉 Optimization mindset 👉 Confidence in solving Hard-level problems 90 days to go 🚀 #100DaysOfCode #LeetCode #DSA #Arrays #CyclicSort #Java #ProblemSolving #TechGrowth #AIML
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
-
Spent these past few days working through foundational array problems - and it turned out to be a solid revision of patterns that repeatedly show up in problem solving. Here’s a structured refresher based on what I practiced: 🔹 Basic Traversal Patterns Problems like finding the largest/second largest element or checking if an array is sorted reinforce one thing: 👉 Most array problems start with how you traverse and track state efficiently. 🔹 In-place Transformations • Remove duplicates from a sorted array • Move all zeros to the end • Left rotate / rotate by K These highlight the importance of the two-pointer technique - one pointer to read, one to write, and reversal-based approaches. - helping achieve O(n) time and O(1) space. 🔹 Working with Sorted Arrays • Union of two sorted arrays This feels very similar to the merge step of merge sort - 👉 leveraging sorted order to avoid extra work. 🔹 Missing / Unique Elements • Find missing number (1 to N) • Find element appearing once (others twice) Key insight: 👉 Problems often have hidden mathematical or bit-manipulation optimizations (e.g., sum formula, XOR properties). 🔹 Subarray Problems (Most Insightful) • Largest subarray with sum K (positive numbers) • Longest subarray with sum K (including negatives) This is where patterns really differentiate: • For positive numbers only -> Sliding Window works efficiently • With negatives involved -> Need Prefix Sum + HashMap 👉 Choosing the right approach depends entirely on input constraints. 💡 Big takeaway from this week: Many array problems are not about new logic every time, but about recognizing which pattern to apply: • Two pointers • Sliding window • Prefix sum + hashing Once these click, a lot of problems become variations of the same idea. I’ll be continuing with arrays for the next couple of weeks, moving into medium and harder problems - looking forward to seeing how these fundamentals scale. #DSA #Arrays #ProblemSolving #Java #LearningInPublic #Algorithms #SoftwareEngineering #SoftwareDevelopment
To view or add a comment, sign in
-
Day 81 - LeetCode Journey 🚀 Solved LeetCode 2: Add Two Numbers (Medium) — a classic linked list problem that beautifully combines arithmetic with pointer manipulation. At first, it looks like simple addition. But the twist is that numbers are stored in reverse order as linked lists, and we must simulate digit-by-digit addition. 💡 Core Idea (Simulation + Carry Handling): Traverse both linked lists simultaneously Add corresponding digits along with carry Create a new node for each digit of the result Move forward until both lists and carry are exhausted A dummy node helps in building the result list smoothly. 🤯 Why it works? Because we mimic the exact process of manual addition, handling carry at each step, ensuring correctness even when lengths differ. ⚡ Key Learning Points: • Converting real-world logic into code (digit addition) • Handling carry efficiently • Traversing two linked lists together • Using dummy node for clean construction • Managing different list lengths gracefully • Achieving O(max(n, m)) time and O(1) extra space This problem strengthens both logical thinking and linked list handling. Also, this pattern connects with: Add Binary Multiply Strings Linked List arithmetic problems Big integer calculations ✅ Better understanding of simulation problems ✅ Stronger pointer + arithmetic combination ✅ Improved handling of edge cases (carry, unequal lengths) From simple addition to linked list manipulation — this is where logic meets implementation 🚀 #LeetCode #DSA #Java #LinkedList #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 2 of #100DaysOfDSA — Consistency in Action Today’s Problem: Product of Array Except Self (LeetCode 238) Difficulty: Medium At first glance, this problem looks simple… until you see the constraints 👀 ❌ Division is NOT allowed ⚡ Solution must run in O(n) time 🧠 Must handle zero values correctly 💡 Key Insight: Prefix + Suffix Product Technique Instead of calculating the product separately for each index (which would be slow), I learned how to precompute: ➡️ Product of elements to the LEFT of each index ➡️ Product of elements to the RIGHT of each index Then combine both to get the final answer. This approach reduces the complexity from O(n²) ➝ O(n) 🔥 📌 Example Input: [1, 2, 3, 4] Output: [24, 12, 8, 6] Each position contains the product of all elements except itself. 🎯 What I Learned Today ✅ How to avoid division using prefix & suffix arrays ✅ How to optimize space by reusing the output array ✅ Importance of thinking in terms of cumulative contributions ✅ A very common interview pattern for array problems 💭 Big Takeaway: Many “hard-looking” problems become simple once you think from both directions. Consistency > Motivation 💪 Day 2 Complete ✔️ On to Day 3 🔥 #100DaysOfDSA #LeetCode #DataStructures #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Java #Tech
To view or add a comment, sign in
-
-
🔥 Day-9 — Rotate Array (Array Manipulation + Reversal Trick) 🧩 Problem: Rotate Array 💻 Platform: LeetCode (#189) Given an array, rotate it to the right by k steps. At first, this looks like a simple shifting problem. Brute force idea: Shift elements one by one, k times. Time Complexity → O(n × k). Not scalable. Better idea: Use an extra array and place elements at correct rotated positions. Time → O(n) Space → O(n) But the optimal solution? 🚀 Reverse Technique (In-Place) Steps: 1️⃣ Reverse the entire array 2️⃣ Reverse first k elements 3️⃣ Reverse remaining n − k elements That’s it. Time Complexity: O(n) Space Complexity: O(1) 💡 Why this works: Reversal repositions elements into their final rotated order without needing extra memory. It’s not obvious at first — but once you visualize it, it becomes clean and elegant. Key Takeaways: ✔ Not all array problems require extra space ✔ In-place manipulation is a powerful skill ✔ Sometimes the trick is not shifting — but reordering cleverly Each day I’m realizing: Patterns repeat. Tricks repeat. Only the surface changes. Day-9 complete. Onward 🚀 #30DaysOfCode #LeetCode #DSA #Java #Arrays #Algorithms #ProblemSolving #SoftwareEngineering #Developers
To view or add a comment, sign in
-
-
🚀 Day 544 of #750DaysOfCode 🚀 🧩 Problem Solved: Matrix Similarity After Cyclic Shifts Today’s problem focused on understanding how cyclic shifts affect matrix rows and optimizing the approach instead of brute-force simulation. 🔍 Key Insight: Instead of performing the shift operation k times, we can reduce it using: 👉 k % n (where n = number of columns) This works because after n shifts, the row returns to its original form. 💡 Approach: Even-indexed rows → shift left Odd-indexed rows → shift right Compare each element with its expected position after shifting No need to actually modify the matrix ✅ ⚡ Optimization: Avoided unnecessary operations by directly checking positions using modulo arithmetic. 📊 Complexity: Time: O(m × n) Space: O(1) 🎯 What I Learned: Efficient problem solving is not about doing more operations, but about reducing them smartly. Recognizing patterns like cyclic repetition can drastically improve performance. 💬 Question for you: Have you ever optimized a brute-force solution using mathematical insights like modulo? #LeetCode #DataStructures #Algorithms #Java #CodingJourney #ProblemSolving #TechInterview #100DaysOfCode #LearningInPublic
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
-
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