Formatted question description: https://leetcode.ca/all/960.html

# 960. Delete Columns to Make Sorted III (Hard)

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array A = ["babca","bbazb"] and deletion indices {0, 1, 4}, then the final array after deletions is ["bc","az"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has every element (row) in lexicographic order.

For clarity, A is in lexicographic order (ie. A <= A <= ... <= A[A.length - 1]), A is in lexicographic order (ie. A <= A <= ... <= A[A.length - 1]), and so on.

Return the minimum possible value of D.length.

Example 1:

Input: ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is A = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. A <= A and A <= A).
Note that A > A - the array A isn't necessarily in lexicographic order.


Example 2:

Input: ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.


Example 3:

Input: ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.


Note:

1. 1 <= A.length <= 100
2. 1 <= A[i].length <= 100

Related Topics:
Dynamic Programming

## Solution 1. DP

Let dp[i] be the maximum number of columns that are non-decreasing in the first i + 1 columns.

dp[i] = max( 1, max( dp[j] + 1 | column j can go after column i ) )

// OJ: https://leetcode.com/problems/delete-columns-to-make-sorted-iii/
// Time: O(M * N^2)
// Space: O(N)
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int M = A.size(), N = A.size(), ans = N - 1, k;
vector<int> dp(N, 1);
for (int i = 1; i < N; ++i) {
for (int j = 0; j < i; ++j) {
for (k = 0; k < M; ++k) {
if (A[k][j] > A[k][i]) break;
}
if (k == M) dp[i] = max(dp[i], dp[j] + 1);
}
ans = min(ans, N - dp[i]);
}
return ans;
}
};


Java

• class Solution {
public int minDeletionSize(String[] A) {
int rows = A.length, columns = A.length();
int[] dp = new int[columns];
Arrays.fill(dp, 1);
for (int i = columns - 2; i >= 0; i--) {
for (int j = i + 1; j < columns; j++) {
boolean flag = true;
for (int k = 0; k < rows; k++) {
String str = A[k];
if (str.charAt(i) > str.charAt(j)) {
flag = false;
break;
}
}
if (flag)
dp[i] = Math.max(dp[i], 1 + dp[j]);
}
}
int remain = 0;
for (int num : dp)
remain = Math.max(remain, num);
return columns - remain;
}
}

• // OJ: https://leetcode.com/problems/delete-columns-to-make-sorted-iii/
// Time: O(M * N^2)
// Space: O(N)
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int M = A.size(), N = A.size(), ans = N - 1, k;
vector<int> dp(N, 1);
for (int i = 1; i < N; ++i) {
for (int j = 0; j < i; ++j) {
for (k = 0; k < M; ++k) {
if (A[k][j] > A[k][i]) break;
}
if (k == M) dp[i] = max(dp[i], dp[j] + 1);
}
ans = min(ans, N - dp[i]);
}
return ans;
}
};

• # 960. Delete Columns to Make Sorted III
# https://leetcode.com/problems/delete-columns-to-make-sorted-iii/

class Solution:
def minDeletionSize(self, A: List[str]) -> int:
n = len(A)
dp =  * n

for j in range(1, n):
for i in range(j):
if all(a[j] >= a[i] for a in A):
dp[j] = max(dp[j], dp[i] + 1)

return n - max(dp)