Welcome to Subscribe On Youtube

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

955. Delete Columns to Make Sorted II (Medium)

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 = ["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 D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]).

Return the minimum possible value of D.length.

 

Example 1:

Input: ["ca","bb","ac"]
Output: 1
Explanation: 
After deleting the first column, A = ["a", "b", "c"].
Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]).
We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1.

Example 2:

Input: ["xc","yb","za"]
Output: 0
Explanation: 
A is already in lexicographic order, so we don't need to delete anything.
Note that the rows of A are not necessarily in lexicographic order:
ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...)

Example 3:

Input: ["zyx","wvu","tsr"]
Output: 3
Explanation: 
We have to delete every column.

 

Note:

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

Companies:
Google

Related Topics:
Greedy

Solution 1. Greedy

// OJ: https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
    int minDeletionSize(vector<string>& A) {
        int M = A.size(), N = A[0].size(), ans = 0;
        vector<bool> done(M, false);
        for (int j = 0, i; j < N; ++j) {
            for (i = 1; i < M; ++i) {
                if (!done[i] && A[i][j] < A[i - 1][j]) break;
            }
            if (i == M) {
                int cnt = 0;
                for (i = 1; i < M; ++i) {
                    cnt += (done[i] = done[i] || A[i][j] > A[i - 1][j]);
                }
                if (cnt == M - 1) break;
            } else ++ans;
        }
        return ans;
    }
};
  • class Solution {
        public int minDeletionSize(String[] A) {
            int rows = A.length, columns = A[0].length();
            if (rows < 2)
                return 0;
            int deletionSize = 0;
            int[] differences = new int[rows - 1];
            for (int i = 0; i < columns; i++) {
                int[] curDifferences = new int[rows - 1];
                boolean deleteFlag = false;
                for (int j = 0; j < rows - 1; j++) {
                    if (differences[j] == 0) {
                        if (A[j].charAt(i) > A[j + 1].charAt(i)) {
                            deletionSize++;
                            deleteFlag = true;
                            break;
                        } else if (A[j].charAt(i) < A[j + 1].charAt(i))
                            curDifferences[j] = 1;
                    }
                }
                if (!deleteFlag) {
                    for (int j = 0; j < rows - 1; j++)
                        differences[j] = Math.max(differences[j], curDifferences[j]);
                }
            }
            return deletionSize;
        }
    }
    
    ############
    
    class Solution {
        public int minDeletionSize(String[] A) {
            if (A == null || A.length <= 1) {
                return 0;
            }
            int len = A.length, wordLen = A[0].length(), res = 0;
            boolean[] cut = new boolean[len];
        search:
            for (int j = 0; j < wordLen; j++) {
                // 判断第 j 列是否应当保留
                for (int i = 0; i < len - 1; i++) {
                    if (!cut[i] && A[i].charAt(j) > A[i + 1].charAt(j)) {
                        res += 1;
                        continue search;
                    }
                }
                // 更新 cut 的信息
                for (int i = 0; i < len - 1; i++) {
                    if (A[i].charAt(j) < A[i + 1].charAt(j)) {
                        cut[i] = true;
                    }
                }
            }
            return res;
        }
    }
    
  • // OJ: https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
    // Time: O(MN)
    // Space: O(MN)
    class Solution {
    public:
        int minDeletionSize(vector<string>& A) {
            int M = A.size(), N = A[0].size(), ans = 0;
            vector<bool> done(M, false);
            for (int j = 0, i; j < N; ++j) {
                for (i = 1; i < M; ++i) {
                    if (!done[i] && A[i][j] < A[i - 1][j]) break;
                }
                if (i == M) {
                    int cnt = 0;
                    for (i = 1; i < M; ++i) {
                        cnt += (done[i] = done[i] || A[i][j] > A[i - 1][j]);
                    }
                    if (cnt == M - 1) break;
                } else ++ans;
            }
            return ans;
        }
    };
    
  • # 955. Delete Columns to Make Sorted II
    # https://leetcode.com/problems/delete-columns-to-make-sorted-ii/
    
    class Solution:
        def minDeletionSize(self, strs: List[str]) -> int:
            m = len(strs)
            n = len(strs[0])
            words = ["" for x in strs]
            res = 0
            
            for i in range(n):
                for index, word in enumerate(strs):
                    words[index] += word[i]
                
                if any(A > B for A, B in zip(words, words[1:])):
                    res += 1
                    for index in range(m):
                        words[index] = words[index][:-1]
            
            return res
    
    

All Problems

All Solutions