🧩 LeetCode POTD: Delete Columns to Make Sorted II So, what is this problem actually asking? 🤔 We’re given a few strings, all of the same length. We’re allowed to delete columns (meaning: remove the same index from every string). After deleting, the strings as a whole should be in dictionary (lexicographic) order 📖 Our goal is to delete the minimum number of columns. 💡 Intuition When we compare two strings, we go left to right. The first character where they differ decides the order. So if at some column we find: strs[i] < strs[i+1] 👉 That pair is sorted forever ✅ No later column can change this. Because of this: We should remember which adjacent string pairs are already sorted and stop checking them again and again. ⚡ Optimization : Instead of rebuilding strings every time: Go column by column (left to right) Keep a sorted[] array sorted[i] = true → strs[i] and strs[i+1] are already in the correct order 😊 If a column breaks order for any unsorted pair......delete that column Otherwise okay, use this column to mark more pairs as sorted #leetcode #potd #problem_solving #optimization #coding
Delete Columns to Make Sorted II LeetCode Challenge
More Relevant Posts
-
When a LeetCode "Hard" is just a classic algorithm in disguise. I just hit a 60-day streak on LeetCode! To celebrate, I tackled problem 960. Delete Columns to Make Sorted III. . . . . . . . . . . At first glance, it asks for the minimum number of columns to delete to ensure every row in a grid is sorted. It sounds complicated, but this is a perfect example of why pattern recognition is key in competitive programming. The shift in perspective: Instead of thinking about what to delete, I flipped the problem: What is the maximum number of columns I can keep? The Algorithm: This reduces perfectly to the Longest Increasing Subsequence (LIS) pattern. Standard LIS: We check if nums[j] <= nums[i] to extend a subsequence. The Modification: Here, we treat each column as an element. For column i to follow column j, the character at j must be $\le$ the character at i for every single row. Once you apply that constraint to the standard $O(N^2)$ DP approach, the solution becomes clear.Result = Total Columns - Length of LIS. It’s satisfying when a "Hard" problem turns "Easy" just by recognizing the underlying structure. Consistency pays off! #LeetCode #Algorithms #CPlusPlus #DynamicProgramming #Consistency #ProblemSolving #TCS
To view or add a comment, sign in
-
-
Happy new year everyone ! Before the end of 2025 I managed to make the Polymorph assembly compiler able to compile the 7 most central instructions to bincode - and make the VM able to execute them. That means, that it is now possible to write super-simple scripts for the Polymorph VM ! The Polymorph Assembly language is currently way too verbose to use for practical purposes - but I still think it is important to have - to be able to decompile bincode and see what is going on. Look at this example: beginExpressionStackScope() expressionStackPush("value1;) beginFunctionStackScope() expressionStackTopToFunctionStack() callExternalFunction("func1;) endFunctionStackScope() endExpressionStackScope() These instructions simply carry out a single function call to an external function plugged into the VM. Something we are used to seeing written in script languages as concisely as this: func1("value1") I will be adding some syntax shortcuts, as well as more instructions, in 2026, so the Polymorph assembly language gets easier, more concise and more powerful to use. Maybe there should be MCP support so you could interact with Polymorph using an LLM 🤔 ... but first the basic functionality needs to be there. Eventually you will be able to plugin a set of functions (an API) into the Polymorph VM - and then script interaction with this API. The scripts can originate from outside your application - e.g. from files - or you can receive them as requests over a network. Using the Polymorph VM you could turn your application or microservices into a lightly scriptable micro-platform - giving your users a lot more flexibility in how they use your application or services . I will be talking about this more in 2026 😊 #Polymorph #PolymorphPersonalComputePlatform
To view or add a comment, sign in
-
26). 😊 🚀 LeetCode #283 – Move Zeroes | Two Pointers | C++ Solved LeetCode Problem 283, which focuses on in-place array manipulation using the Two Pointers technique. 🔍 Problem Overview Given an integer array nums, move all 0s to the end of the array while maintaining the relative order of the non-zero elements. 📌 Example Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] It uses two pointers: f (fast) scans through the array from left to right, and s (slow) tracks the position where the next non-zero element should be placed. As the fast pointer moves, whenever it encounters a non-zero value, that value is swapped with the element at the slow pointer’s position, and the slow pointer is then advanced. Zero values are simply skipped by the slow pointer and naturally end up toward the end of the array. By the time the fast pointer reaches the end, all non-zero elements have been shifted to the front in order, and all zeroes are moved to the back. ⚙️ Complexity Analysis ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 📌 Language: C++ 📌 Platform: LeetCode #LeetCode #CPlusPlus #DSA #Arrays #TwoPointers #InPlaceAlgorithm #ProblemSolving #CodingPractice #CompetitiveProgramming #SoftwareEngineering #ComputerScience #CSStudents #BCA #BCAStudents #InterviewPreparation #DailyCoding #LearningToCode #DeveloperJourney #TechJourney #Consistency #CodingLife
To view or add a comment, sign in
-
-
Leetcode POTD: Delete Columns to Make Sorted III So this is part 3 of the “delete columns to make it sorted” series, but with a different twist. Here, we need to delete columns in such a way that each individual string becomes sorted. It sounds easy at first, but it’s really not 😅 I initially tried a greedy approach and failed, and after a few trials, I finally reached the correct intuition. Thought process: I started by iterating over the columns, checking where the order breaks and incrementing a counter. But this greedy approach eventually failed, because there are test cases where, say, column 3 is bad (it violates the condition) with column 2, so I decide to delete it. However, that same column 3 might actually be valid with column 1, so deleting it is not optimal ❌ Also, we need to preserve the sorted order, which clearly hints towards using DP. We cannot choose a local minimum because it can negatively impact the global solution. This starts to resemble the LIS (Longest Increasing Subsequence) pattern 📈 So, using DP makes the solution feasible ✅ Overall, it’s a hard question, because identifying this intuition is difficult unless you’ve solved a lot of problems, especially string-based ones 🧠✨ 📌 Attaching my notes below, which walk through the step-by-step intuition, explain why the greedy approach fails, and how the DP (LIS-style) solution works. #coding #programming #potd #leetcode
To view or add a comment, sign in
-
-
🚀LeetCode Daily Challenge 🧩Problem: Max Dot Product of Two Subsequences You are given two integer arrays nums1 and nums2. You need to choose non-empty subsequences from both arrays such that their dot product is maximized. 🎯 Goal: Return the maximum dot product that can be obtained from any valid pair of subsequences. 🧠 Key Intuition This is a Dynamic Programming problem with choices at each index: We can either pair nums1[i] with nums2[j] Or skip an element from either array Important observation: Since subsequences must be non-empty, we cannot default to 0 Negative values matter, so we initialize invalid states with a very small number At each state (i, j), we consider: Taking both elements → nums1[i] × nums2[j] Extending a previous subsequence Skipping an element from either array 🛠 Approach Use recursion + memoization Define dp[i][j] as the maximum dot product starting from indices i and j Transitions: Take current pair and continue Skip from nums1 Skip from nums2 Base case ensures at least one pair is chosen 📈 Complexity Analysis Time Complexity: O(m × n) Space Complexity: O(m × n) 🔗 Problem https://lnkd.in/d4EJeckq 💻 Solution https://lnkd.in/dgxyD7Uy #LeetCode #Cpp #DSA #DynamicProgramming #Memoization #Recursion #Arrays #ProblemSolving #CodingChallenge #LeetCodeDailyChallenge #Algorithms
To view or add a comment, sign in
-
🚀 LeetCode + DSA — Day 20 Today I solved LeetCode 75: Sort Colors, a classic problem that strengthens in-place sorting and comparison-based logic. 🔹 Problem Overview You’re given an array containing only 0, 1, and 2, representing three different colors. The task is to sort the array in-place so that elements of the same color are adjacent and ordered as 0 → 1 → 2, without using the built-in sort function. 🔹 Approach Used ✔ Used a comparison-based in-place sorting strategy ✔ Iterated through the array and tracked the smallest element index ✔ Swapped elements to position them correctly ✔ Ensured no extra space was used 🔹 Why This Works In-place swapping avoids additional memory usage Controlled iteration guarantees correct ordering Simple logic, yet fully compliant with problem constraints 📊 Performance ✅ All test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: Efficient in-place solution 💡 Key Takeaway Problems with strict constraints push you to think beyond library functions. Mastering in-place operations builds strong fundamentals for real-world systems where space efficiency matters. Consistent practice > complex solutions 💪 #LeetCode #DSA #Python #Arrays #Sorting #InPlaceAlgorithm #ProblemSolving #DailyCoding #CodingPractice #SoftwareDeveloperJourney #LearningByDoing
To view or add a comment, sign in
-
-
Day 8: Roman to Integer (Cracking the Subtractive Rule) Consistency is key! Today, I tackled the Roman to Integer challenge on LeetCode. While it looks like a simple mapping task, the real challenge lies in handling the subtractive instances (like IV = 4, not 6). 🧠 The Logic: Look-Ahead Strategy The standard rule for Roman numerals is addition. However, when a smaller numeral precedes a larger one, you subtract it. I implemented a look-ahead check: Iterate through the string character by character. If the current value is less than the next value (e.g., 'I' before 'V'), subtract the current value from the total. Otherwise, add it to the total. ⚡ Performance Results I’m thrilled with the efficiency of this solution: Runtime: 1 ms (Beats 99.77% of C# submissions!) 🚀 Memory: 49.07 MB (Beats 93.46% of C# submissions!) Time Complexity: $O(N)$ Space Complexity: $O(1)$ Sharing these daily updates keeps me accountable and hopefully helps others on their DSA journey. If you’re also practicing for interviews, let’s connect! #DSA #LeetCode #CSharp #CodingLife #Programming #ProblemSolving #SoftwareEngineer
To view or add a comment, sign in
-
#Day9of2026 | LeetCode Practice 📌 Problem: 85 – Maximal Rectangle 🔗 Link (POTD-ready): https://lnkd.in/gYk_b4Pf 📚 Concept: Stack, Histogram Technique, Dynamic Programming on Matrix 💡 Intuition: Finding the largest rectangle of 1s in a binary matrix looks like a 2D problem, but it becomes much simpler when transformed into repeated 1D problems. ->For each row, we treat it as the base of a histogram: ->Each column stores the height of consecutive 1s ending at the current row. ->If a cell is 0, the height resets to zero. ->If it is 1, the height increases. ->Once the histogram is built for a row, the problem reduces to the classic Largest Rectangle in Histogram, which can be solved efficiently using a monotonic stack. By repeating this process for every row and taking the maximum area, we cover all possible rectangles in the matrix. 🧠 Key Learning: - A 2D matrix problem can often be reduced into multiple 1D histogram problems. - The monotonic stack pattern is crucial for solving rectangle-area problems optimally. 📎 Sharing how histogram transformation unlocks an efficient stack-based solution. https://lnkd.in/gpZawPb5 #LeetCode #DSA #Stack #MonotonicStack #DynamicProgramming #Consistency #LearningInPublic #LeetCodeDaily #JavaDSA #Day9 #LeetCode85 #JavaDSA #Java
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
great 👍