Problem Description
Given an array of n strings of equal length, we are allowed to delete any set of columns. After deletion, each string (i.e., each row) must have its remaining characters in non-decreasing (i.e., lexicographic) order. The goal is to determine the minimum number of columns that need to be deleted so that every row is individually sorted.
Key Insights
- The problem can be reinterpreted as selecting the largest sequence of columns to keep such that, for every row, the characters in the selected columns are in non-decreasing order.
- This is analogous to finding the Longest Increasing Subsequence (LIS) where each "element" (column) must be compatible with previously chosen columns across all rows.
- We use dynamic programming: for each column, consider previous columns that can be appended while maintaining the sorted property for every row.
- Finally, the answer is the total number of columns minus the length of this maximum valid subsequence (i.e., columns to keep).
Space and Time Complexity
Time Complexity: O(m^2 * n)
- m is the number of columns and n is the number of rows; in the worst-case, we compare each pair of columns across all rows. Space Complexity: O(m)
- We maintain a DP array of size m.
Solution
The approach is to use dynamic programming to compute the maximum number of columns we can keep such that every row is non-decreasing in the kept columns. For each column j (from 0 to m - 1), we iterate over every previous column i (from 0 to j - 1) and check if, for every row, the character in column i is less than or equal to that in column j. If the condition holds, column j can extend the valid sequence ending at column i. We update dp[j] accordingly. The final answer is obtained by subtracting the length of the longest valid subsequence from the total number of columns.
This method leverages a simple comparison between columns over all rows, ensuring that the lexicographic condition (non-decreasing order) holds for every string independently.