码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode 955] Delete Columns to Make Sorted II

时间:2020-02-04 10:24:21      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:following   roc   proc   ril   return   determine   element   NPU   eterm   

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

 

Key observation: At the same index j of two consecutive strings s1 and s2, if s1[j] < s2[j], there will be no need to check further as s1 is definitively smaller than s2. If s[i] > s[j], then we must delete this column j. If s[i] == s[j],  we can keep this column but we need more checks to determine if s1 and s2 are in the correct relative order. 

 

Based on above observation, we derive the following algorithm.

1. Keep a boolean array B to store the relative order of consecutive strings.

2. For each column j, if two consecutive strings s1 and s2 (s1 appears before s2) are not in sorted order already and s1[j] > s2[j], delete this column. Proceed to the next column without updating B as this column is deleted.

3. Otherwise, we keep the current column and update B using the current column‘s comparison results. If all strings are already in sorted order, terminate.

 

 

class Solution {
    public int minDeletionSize(String[] A) {
        int n = A.length, len = A[0].length();
        int delCount = 0;
        boolean[] sorted = new boolean[n];
        sorted[0] = true;
        for(int charIdx = 0; charIdx < len; charIdx++) {
            int strIdx = 1;
            for(; strIdx < n; strIdx++) {
                if(!sorted[strIdx] && A[strIdx].charAt(charIdx) < A[strIdx - 1].charAt(charIdx)) {
                    delCount++;
                    break;
                }
            }
            if(strIdx == n) {
                boolean terminate = true;
                for(strIdx = 1; strIdx < n; strIdx++) {
                    sorted[strIdx] |= A[strIdx - 1].charAt(charIdx) < A[strIdx].charAt(charIdx);
                    terminate &= sorted[strIdx];
                }           
                if(terminate) {
                    break;
                }
            }
        }
        return delCount;
    }
}

 

 

 

[LeetCode 955] Delete Columns to Make Sorted II

标签:following   roc   proc   ril   return   determine   element   NPU   eterm   

原文地址:https://www.cnblogs.com/lz87/p/12258287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!