Leetcode POTD: Delete Columns to Make Sorted III with DP

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

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories