960. Delete Columns to Make Sorted III

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.length
  • 1 <= n <= 100
  • 1 <= strs[i].length <= 100
  • strs[i] consists of lowercase English letters.

From: LeetCode
Link: 960. Delete Columns to Make Sorted III


Solution:

Ideas:
  • Each column acts like a “character position” across all rows.

  • We want to keep the longest sequence of columns such that every row is non-decreasing when reading only those kept columns.

  • This is the same as finding the Longest Increasing Subsequence (LIS) over columns,
    where column j can come before column i only if for all rows:
    strs[row][j] <= strs[row][i].

  • Use DP: dp[i] = longest valid sequence ending at column i.

  • Try all pairs (j < i) to see if column j can precede column *i`.

  • Track the longest valid sequence length = best.

  • Minimum deletions = total columns − best.

Code:
int minDeletionSize(char** strs, int strsSize) {
    if (strsSize == 0) return 0;
    
    int m = strlen(strs[0]);  // number of columns
    int dp[101];              // dp[i] = longest valid sequence ending at column i
    int i, j, r;
    
    for (i = 0; i < m; ++i) {
        dp[i] = 1;  // each column alone is a valid sequence
    }
    
    int best = 1;
    
    for (i = 0; i < m; ++i) {
        for (j = 0; j < i; ++j) {
            int ok = 1;
            // Check if column j can come before i for all rows
            for (r = 0; r < strsSize; ++r) {
                if (strs[r][j] > strs[r][i]) {
                    ok = 0;
                    break;
                }
            }
            if (ok && dp[j] + 1 > dp[i]) {
                dp[i] = dp[j] + 1;
            }
        }
        if (dp[i] > best) best = dp[i];
    }
    
    return m - best;  // delete all other columns
}
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐