zoukankan      html  css  js  c++  java
  • [LeetCode 955] Delete Columns to Make Sorted II

    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;
        }
    }
  • 相关阅读:
    The bean 'xxx' could not be injected as a 'xxx'because it is a JDK dynamic proxy that implements
    JQuery对象选择详细说明
    C#将image中的显示的图片转换成二进制
    选择一个图片文件,并且用PictureBox表示在Form上
    Android推送方案分析(MQTT/XMPP/GCM)
    [转载]什么是 Design Hackathon?
    脚本启动MySql服务
    Mysql数据库插入数据乱码问题,解决方案!
    MySQL自动备份脚本
    读取Excel模板,写入数据,并别输出另存为Excel文件
  • 原文地址:https://www.cnblogs.com/lz87/p/12258287.html
Copyright © 2011-2022 走看看