🧱 𝗜 𝗦𝗼𝗹𝘃𝗲𝗱 𝗮 “𝗩𝗶𝘀𝘂𝗮𝗹” 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗨𝘀𝗶𝗻𝗴 𝗢𝗻𝗹𝘆 𝗮 𝗦𝘁𝗮𝗰𝗸 — 𝗡𝗼 𝗚𝗲𝗼𝗺𝗲𝘁𝗿𝘆 𝗡𝗲𝗲𝗱𝗲𝗱 Today’s problem looked visual and innocent: 𝗚𝗶𝘃𝗲𝗻 𝗯𝗮𝗿 𝗵𝗲𝗶𝗴𝗵𝘁𝘀 𝗼𝗳 𝗮 𝗵𝗶𝘀𝘁𝗼𝗴𝗿𝗮𝗺 (𝘄𝗶𝗱𝘁𝗵 = 𝟭), 𝗳𝗶𝗻𝗱 𝘁𝗵𝗲 𝗹𝗮𝗿𝗴𝗲𝘀𝘁 𝗿𝗲𝗰𝘁𝗮𝗻𝗴𝗹𝗲 𝗮𝗿𝗲𝗮. This is the classic problem from 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 — and it’s a beautiful lesson in how 𝗺𝗼𝗻𝗼𝘁𝗼𝗻𝗶𝗰 𝘀𝘁𝗮𝗰𝗸𝘀 𝗰𝗼𝗻𝘃𝗲𝗿𝘁 𝗴𝗲𝗼𝗺𝗲𝘁𝗿𝘆 𝗶𝗻𝘁𝗼 𝗶𝗻𝗱𝗶𝗰𝗲𝘀. 🧠 𝗧𝗵𝗲 𝗡𝗮𝗶𝘃𝗲 𝗧𝗵𝗼𝘂𝗴𝗵𝘁 (𝗮𝗻𝗱 𝘄𝗵𝘆 𝗶𝘁 𝗳𝗮𝗶𝗹𝘀) For every bar i, try expanding left and right until you hit a smaller bar. That’s O(n) per index -> O(n²). Too slow for n=10^5. So the real question becomes: For each bar, how far can it extend without hitting a smaller height? That is the entire problem. 💡 𝗞𝗲𝘆 𝗜𝗱𝗲𝗮 — 𝗡𝗲𝗮𝗿𝗲𝘀𝘁 𝗦𝗺𝗮𝗹𝗹𝗲𝗿 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 For every index i: 1. Find the first smaller bar on the left 2. Find the first smaller bar on the right A monotonic increasing stack gives both in linear time. ▶️ 𝗟𝗲𝗳𝘁 𝗦𝗰𝗮𝗻 (𝗟 -> 𝗥) Pop until the top is strictly smaller. That index defines the left boundary. ◀️ 𝗥𝗶𝗴𝗵𝘁 𝗦𝗰𝗮𝗻 (𝗥 -> 𝗟) Same logic to get the right boundary. Now each bar knows the exact range where it is the minimum. ⏱️ 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 Each index pushed/popped once → O(n) Space → O(n) ✨ 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴𝘀 1. “Nearest smaller element” is a powerful pattern 2. Monotonic stacks turn nested loops into linear scans 3. Think: If this bar is the minimum, how wide can the rectangle be? A visual problem, solved with clean stack discipline. #DataStructures #Algorithms #MonotonicStack #Java #ProblemSolving #CodingInterview #Stack #DSA #LeetCode #Coding #Programmer #SoftwareEngineer #InterviewPrep #CompetitiveProgramming #100DaysOfCode #Tech #Learning #ProblemSolvingSkills
Visual Problem Solved with Monotonic Stack
More Relevant Posts
-
🧩 𝗦𝘁𝗼𝗽 𝗦𝗲𝗮𝗿𝗰𝗵𝗶𝗻𝗴 𝗥𝗲𝗰𝘁𝗮𝗻𝗴𝗹𝗲𝘀: 𝗧𝗵𝗲 𝗛𝗶𝘀𝘁𝗼𝗴𝗿𝗮𝗺 𝗠𝗶𝗻𝗱𝘀𝗲𝘁 𝗧𝗵𝗮𝘁 𝗦𝗼𝗹𝘃𝗲𝘀 𝗧𝗵𝗶𝘀 𝗠𝗮𝘁𝗿𝗶𝘅 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Solved an interesting problem today on 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲: 𝗙𝗶𝗻𝗱 𝘁𝗵𝗲 𝗮𝗿𝗲𝗮 𝗼𝗳 𝘁𝗵𝗲 𝗹𝗮𝗿𝗴𝗲𝘀𝘁 𝗿𝗲𝗰𝘁𝗮𝗻𝗴𝗹𝗲 𝗰𝗼𝗻𝘁𝗮𝗶𝗻𝗶𝗻𝗴 𝗼𝗻𝗹𝘆 𝟭’𝘀 𝗶𝗻 𝗮 𝗯𝗶𝗻𝗮𝗿𝘆 𝗺𝗮𝘁𝗿𝗶𝘅. At first glance, this looks like a classic 𝟮𝗗 𝗿𝗲𝗰𝘁𝗮𝗻𝗴𝗹𝗲 𝘀𝗲𝗮𝗿𝗰𝗵. But the elegant solution doesn’t search rectangles at all. It 𝗯𝘂𝗶𝗹𝗱𝘀 𝘁𝗵𝗲𝗺 𝘃𝗲𝗿𝘁𝗶𝗰𝗮𝗹𝗹𝘆. 🔄 𝗥𝗼𝘄 𝗯𝘆 𝗥𝗼𝘄 𝗧𝗿𝗮𝗻𝘀𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻 For each row, treat the matrix like this: 1. ‘1’ → increase the column height 2. ‘0’ → reset the column height to 0 After processing a row, you don’t see a matrix anymore. You see a histogram. And now the question becomes: What’s the largest rectangle in this histogram? 🧠 𝗧𝗵𝗲 𝗖𝗹𝗲𝘃𝗲𝗿 𝗧𝘄𝗶𝘀𝘁 Instead of the usual stack approach, maintain three arrays: heights[] → current bar heights leftBoundaries[] → how far left this bar can extend rightBoundaries[] → how far right this bar can extend Two linear scans per row set these boundaries precisely. 📐 𝗔𝗿𝗲𝗮 𝗖𝗼𝗺𝗽𝘂𝘁𝗮𝘁𝗶𝗼𝗻 For every column: width = rightBoundaries[j] - leftBoundaries[j] area = heights[j] * width Update the maximum. Move to the next row. Repeat. 💡 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝘄𝗼𝗿𝗸𝘀 𝗯𝗲𝗮𝘂𝘁𝗶𝗳𝘂𝗹𝗹𝘆 Because for every row, we already know: 1. Continuous height of 1’s above 2. Maximum width possible at that height So we compute the best rectangle in constant time per column. ⚙️ 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 Time: O(rows × cols) Space: O(cols) Optimal. Clean. Elegant. ✨ 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Sometimes the best way to solve a 2D problem is to 𝘀𝘁𝗼𝗽 𝘁𝗵𝗶𝗻𝗸𝗶𝗻𝗴 𝗶𝗻 𝟮𝗗. #Algorithms #DataStructures #LeetCode #Java #CodingInterview #InterviewPrep #DSA #ProblemSolving #CodingLife #Developers #SoftwareEngineer #TechInterview #Programming #LearnToCode #CodeNewbie #CompetitiveProgramming #100DaysOfCode #Debugging #Optimization #LogicBuilding #STEM
To view or add a comment, sign in
-
-
Salam all! I build reusable systems. Not scripts that work once. Templates that save teams hours. Every customer has different rules. Free days. Charge rates. Thresholds. Engineers rewrite the same logic from scratch. I step back. Find the common core. Build templates. Copy it. Plug in the rules. Done. Other engineers start using it. New hires get up to speed faster. That's what I love. Finding patterns. Building once. Helping the next person. #DataEngineering #Python #ReusableCode #EngineeringMindset
To view or add a comment, sign in
-
𝗠𝗼𝘀𝘁 𝗣𝗲𝗼𝗽𝗹𝗲 𝗨𝘀𝗲 𝗔𝗜. 𝗙𝗲𝘄 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 𝗧𝗵𝗲 𝗖𝗼𝗱𝗲 𝗔𝗜 𝗰𝗮𝗻 𝗴𝗲𝗻𝗲𝗿𝗮𝘁𝗲 𝗰𝗼𝗱𝗲. 𝗕𝘂𝘁 𝗶𝗳 𝘆𝗼𝘂 𝗱𝗼𝗻’𝘁 𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 𝗣𝘆𝘁𝗵𝗼𝗻 𝗱𝗲𝗲𝗽𝗹𝘆, 𝘆𝗼𝘂 𝘄𝗼𝗻’𝘁 𝗸𝗻𝗼𝘄 𝘄𝗵𝗮𝘁 𝗶𝘁’𝘀 𝗱𝗼𝗶𝗻𝗴, 𝘄𝗵𝘆 𝗶𝘁 𝗯𝗿𝗲𝗮𝗸𝘀, 𝗼𝗿 𝗵𝗼𝘄 𝘁𝗼 𝘀𝗰𝗮𝗹𝗲 𝗶𝘁. 𝗧𝗵𝗮𝘁’𝘀 𝘄𝗵𝗲𝗿𝗲 𝗿𝗲𝗮𝗹 𝗲𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴 𝘀𝘁𝗮𝗿𝘁𝘀. 𝗕𝗲𝗹𝗼𝘄 𝗮𝗿𝗲 𝘁𝗵𝗲 𝗰𝗼𝗿𝗲 𝘁𝗼𝗽𝗶𝗰𝘀 𝘆𝗼𝘂 𝘀𝗵𝗼𝘂𝗹𝗱 𝗸𝗻𝗼𝘄: 1. Object-Oriented Programming (OOP) 2. Decorators 3. Generators & Iterators 4. Context Managers 5. Async Programming (async/await) 6. Multithreading & Multiprocessing 7. WebSockets 8. Data Structures & Algorithms 9. Memory Management & Garbage Collection 10. File Handling & Serialization 11. List/Dict/Set Comprehensions 12. Exception Handling (advanced patterns) 13. Functional Programming (map, filter, lambda) 14. Modules & Packaging 15. Virtual Environments & Dependency Management 16. Type Hinting & Static Typing 17. Testing (unit tests, mocking, pytest) 18. Logging & Debugging 19. API Development (FastAPI/Flask) 20. Database Handling (SQL/ORMs) Master these, and you’re not just using AI you’re actually building with it. #Python #AI #SoftwareEngineering #Developers #Coding #MachineLearning #Programming #Tech #LearnToCode
To view or add a comment, sign in
-
I have been thinking less about agents and more about paths. A lot of software still assumes execution is: - an app flow - an async mesh - or an agent stack But human work usually looks messier and more real: you carry something, fork, test, return, defer, resume, and only sometimes settle. So I have been exploring a path-style runtime where: - push opens a fork - pop rejoins or abandons a fork - skills attach to the path - memory keeps residue - repeated signals reinforce instead of just duplicating noise The interesting question is not “what is the next token?” It is more like: - what is still being carried? - what changed direction? - what can be deferred? - what actually needs to settle? I may post a tiny Python demo of path execution next. https://lnkd.in/eairkdJK #runtime #systems #paths #softwarearchitecture
To view or add a comment, sign in
-
-
Day 73 on LeetCode Find First and Last Position of Element in Sorted Array 🎯🔍✅ Today’s problem focused on a classic and very important Binary Search variation. 🔹 Approach Used in My Solution The goal was to find the first and last occurrence of a target in a sorted array. Key idea in the solution: • Use two separate binary searches 🔸 Find First Occurrence: • When nums[mid] >= target, move left • If nums[mid] == target, store index and continue searching left 🔸 Find Last Occurrence: • When nums[mid] <= target, move right • If nums[mid] == target, store index and continue searching right • Combine both results to get final answer This ensures we don’t just find a match, but the complete range of the target. ⚡ Complexity: • Time Complexity: O(log n) • Space Complexity: O(1) 💡 Key Takeaways: • Learned how to modify binary search to find boundaries instead of single elements • Strengthened understanding of first/last occurrence patterns • Realized how small logic changes turn binary search into powerful variants 🔥 This pattern is extremely common in interview problems! #LeetCode #DSA #Algorithms #DataStructures #BinarySearch #Arrays #ProblemSolving #Coding #Programming #Cpp #STL #SoftwareEngineering #ComputerScience #CodingPractice #DeveloperLife #TechJourney #CodingDaily #100DaysOfCode #BuildInPublic #AlgorithmPractice #CodingSkills #Developers #TechCommunity #SoftwareDeveloper #EngineeringJourney
To view or add a comment, sign in
-
-
#100DaysOfLeetcode journey 🚀 🚀 Day 76/100 — Tracking Recursive Loops in 2D Space! Today’s Problem: 1559. Detect Cycles in 2D Grid 🔹 The Goal: Given a grid of characters, find if there exists a cycle of length 4 or more consisting of the same value. A cycle is a path that starts and ends at the same cell without moving back to the immediate previous cell. 🔹 The Insight: This is a classic Graph Theory problem applied to a Matrix. To find a cycle, we need to distinguish between "moving forward" and "moving backward." While a standard DFS can find connected components, we need Parent Tracking to identify when we've circled back to a visited node through a different path. 🔹 The Logic: Component Traversal: I used DFS to explore islands of identical characters. Parental Guidance: By passing the (pr, pc) coordinates of the previous cell into the recursion, I ensured the algorithm ignores the immediate backtrack. Cycle Invariant: If we hit a cell that is already visited but is NOT our parent, we've found a loop! In a grid graph, such a loop is guaranteed to have a length of at least 4. ✨ Achievement: Day 76! Moving from coordinate geometry back into graph-based grid traversals. Mastering the "Visited vs. Parent" logic is essential for avoiding infinite loops in complex search algorithms and is a fundamental skill for pathfinding optimizations. 🔍 Steps followed: ✔ Global Visited State: Prevented redundant searches across the $M \times N$ grid. ✔ Directional Vectors: Managed 4-way movement using concise offset arrays. ✔ Recursive Validation: Successfully separated valid cycles from simple path backtracking. 🔧 Complexity Analysis: Time Complexity: $O(M \times N)$ Space Complexity: $O(M \times N)$ (Visited array + Recursion stack) 76 days down! The logic is scaling, the patterns are repeating, and the finish line is getting closer. Let's keep the momentum! 🛠️ #Java #DSA #LeetCode #CodingJourney #100DaysOfCode #SoftwareEngineer #InternshipBound #Programming #DFS #GraphTheory #CycleDetection #Algorithms #Day76
To view or add a comment, sign in
-
-
The Architecture of Logic: Why Your "Instruction Set" Matters More Than Your Language We’ve become obsessed with the "what" of technology: What framework are you using? What library did you import? What language is the most popular this year? But if you strip away the syntax of Python, Rust, or JavaScript, you are left with the only thing that actually matters: The Algorithm. Programming is not "Coding" Coding is just the act of translating human intent into a format a machine understands. True programming is the architecture of logic. It is the ability to take a complex problem and break it down into a finite, definite set of instructions. The Library Trap Libraries are useful "black boxes." They save time, but they can also create a ceiling for your growth. A Coder knows how to call a function from a library. An Engineer knows how to replicate that function’s logic with a pen and paper. When we rely too heavily on pre-packaged tools, we lose the ability to see the system as a whole. We start solving problems by "finding a tool" instead of "designing a solution." The Universal Blueprint An algorithm is universal. If your logic is sound, it doesn't care about the substrate. It works: As a flow of electrical signals in a CPU. As a set of "If-Then" rules on a whiteboard. As a Standard Operating Procedure (SOP) for a human team. If you can’t verify your logic without a computer, you don't own the logic the library does. Final Thought Don't just be an "expert" in a specific keyword or framework. Be an architect of instructions. Master the ability to move data from State A to State B with such clarity that the language you choose becomes the least interesting part of the project. Are you building systems, or are you just calling functions? #ComputerScience #SystemDesign #SoftwareArchitecture #EngineeringMindset #ProblemSolving #Logic
To view or add a comment, sign in
-
-
𝐂𝐥𝐚𝐮𝐝𝐞’𝐬 𝐏𝐲𝐭𝐡𝐨𝐧 𝐜𝐨𝐝𝐞 𝐚𝐠𝐞𝐧𝐭 𝐢𝐬 𝐝𝐨𝐢𝐧𝐠 𝐦𝐨𝐫𝐞 𝐭𝐡𝐚𝐧 𝐦𝐨𝐬𝐭 𝐭𝐞𝐚𝐦𝐬 𝐡𝐚𝐯𝐞 𝐫𝐞𝐚𝐥𝐢𝐳𝐞𝐝 𝐲𝐞𝐭. ⚙️ It doesn’t just write code. It holds context, catches logic gaps early, and suggests meaningful refactors. This isn’t faster coding. It’s a different workflow. The gap is already visible: Faster releases, fewer review cycles, less firefighting, more building. 📈 Teams getting it right treat it as a collaborator, not a shortcut. 𝐀𝐈 𝐡𝐚𝐧𝐝𝐥𝐞𝐬 𝐞𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧. 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐞𝐫𝐬 𝐝𝐫𝐢𝐯𝐞 𝐝𝐞𝐜𝐢𝐬𝐢𝐨𝐧𝐬. The real question isn’t adoption. It’s how much delay you can afford. ⏳ #KarmickSolutions #Python #AIinDevelopment #ClaudeAI #SoftwareDevelopment #TechTrends #Developers #Automation #FutureOfWork
To view or add a comment, sign in
-
I’ve been getting back into solving algorithm problems recently, and I worked through a pretty challenging one: “Delete Columns to Make Sorted III.” At first glance, it looks like a simple string problem. But once you dig in, it becomes a lot more interesting — it’s really about finding a pattern across multiple sequences. 💡 Key idea I learned: Instead of thinking about deleting columns directly, you can reframe the problem as: ➡️ “What is the longest sequence of columns that keeps all strings in sorted order?” Once you see it that way, it turns into a dynamic programming problem — similar to finding a longest increasing subsequence, but across multiple strings. 🧠 What this reinforced for me: Many “hard” problems become easier when you reframe the question Patterns like DP and subsequences show up everywhere Explaining the solution clearly is just as important as solving it I wrote a full breakdown of my approach here: 👉 https://lnkd.in/giN69zAn I’m currently focused on sharpening my problem-solving and backend thinking again. If you’re working through similar problems or preparing for interviews, I’d be interested to hear how you approach these kinds of challenges. #Java #LeetCode #Algorithms #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 33 of My DSA Journey | Optimized Approach 💡 Today I solved one of the classic problems from LeetCode: Merge Sorted Array 🔥 🧩 Problem Understanding Given two sorted arrays nums1 and nums2, we need to merge them into a single sorted array. Constraint: nums1 already has enough space at the end to accommodate elements of nums2. 🐢 Brute Force Approach My first thought was: Copy all elements of nums2 into nums1 Sort the entire array ⏱️ Time Complexity: O((m+n)²) 👉 Works fine, but not efficient for interviews. ⚡ Optimized Approach (Two Pointer - Reverse Merge) Then I explored the optimal solution: 💡 Key Idea: Start filling from the end of nums1 Compare elements from the back of both arrays Place the larger one at the last index 🔁 Steps: Set 3 pointers: i = m-1 (end of valid nums1) j = n-1 (end of nums2) k = m+n-1 (end of nums1 array) Compare and place elements from the back Handle remaining elements of nums2 if any 📌 Example nums1 = [1,2,3,0,0,0] nums2 = [2,5,6] Output: [1,2,2,3,5,6] ⏱️ Complexity Analysis Time: O(m+n) 🚀 Space: O(1) (In-place) 🎯 Key Learning 👉 When arrays are sorted, always think in terms of two pointers 👉 Filling from the end avoids unnecessary shifting 👉 Interviewers expect optimization, not just working code 🙏 Gratitude Grateful for continuous learning and improvement every single day 💯 🔥 Consistency Note Small steps daily → Big results over time 🚀 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode #SoftwareEngineering #InterviewPrep #DataStructures #Algorithms
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