You are given an array of n strings strs, all of the same length.
We may choose any deletion indices, and we delete all the characters in those indices for each string.
For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"].
Suppose we chose a set of deletion indices answer such that after deletions, the final array has every string (row) in lexicographic order. (i.e., (strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1]), and (strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1]), and so on). Return the minimum possible value of answer.length.
Example 1:
Input: strs = ["babca","bbazb"] Output: 3 Explanation: After deleting columns 0, 1, and 4, the final array is strs = ["bc", "az"]. Both these rows are individually in lexicographic order (ie. strs[0][0] <= strs[0][1] and strs[1][0] <= strs[1][1]). Note that strs[0] > strs[1] - the array strs is not necessarily in lexicographic order.
Example 2:
Input: strs = ["edcba"] Output: 4 Explanation: If we delete less than 4 columns, the only row will not be lexicographically sorted.
Example 3:
Input: strs = ["ghi","def","abc"] Output: 0 Explanation: All rows are already lexicographically sorted.
Constraints:
n == strs.length1 <= n <= 1001 <= strs[i].length <= 100strs[i] consists of lowercase English letters.This approach utilizes dynamic programming to identify the minimum columns to delete to make each row lexicographically sorted. The key idea is to use a dp array where dp[j] represents the length of the longest increasing subsequence of columns ending at index j.
We'll iterate over each column and compare with the previous columns to update the dp array by checking each row for their lexicographic order. We finally return the total number of columns minus the maximum value in the dp array, which gives the least number of deletions needed.
The C code implements a dynamic programming solution. It initializes a dp array to track the longest increasing subsequence of column indices which maintain the row order. For each pair of columns, it checks if swapping the columns maintains the lexicographic order in each row. The solution updates the dp array accordingly and returns the total columns minus the maximum value in this array.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2 * m), where n is the length of each string and m is the number of strings.
Space Complexity: O(n) for the dp array.
This approach involves a greedy strategy where we incrementally build the sorted subset of columns. By comparing columns, we find those that necessarily break the order when removed and only retain these to maintain row order.
At each step, the algorithm compares characters in successive columns, choosing deletions based on maintaining an overall least sequence of lexicographically ordered characters per row.
In this greedy C solution, we traverse each column, comparing successive characters for lexicographic order. If for any column, the order breaks, the column is marked for deletion, thereby incrementing the answer tally, which is returned as the result.
C++
Java
Python
C#
JavaScript
Time Complexity: O(m * n), where m is the number of strings and n is the length of each string.
Space Complexity: O(1), as only constant space is used.
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n^2 * m), where n is the length of each string and m is the number of strings. |
| Greedy Approach with Column Comparison | Time Complexity: O(m * n), where m is the number of strings and n is the length of each string. |
Leetcode 960. Delete Columns to make sorted III • Varun Gupta • 2,740 views views
Watch 9 more video solutions →Practice Delete Columns to Make Sorted III with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor