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;
        }
    }
  • 相关阅读:
    【Java学习】Intellij IDEA基本配置
    【Java学习】Integer、new Integer() 和 int 比较和相关的面试题
    【Java学习】String[] args和String args[]的区别在哪里?
    【Java学习】包装类
    【Java学习】Java 枚举(enum)
    【Java学习】eclipse与intellij idea的区别
    【Mysql学习】mysql远程连接:ERROR 1130 (HY000): Host '*.*.*.*' is not allowed to connect to this MySQL server解决办法
    【Mysql学习】Mysql安装
    qplot函数用法(转载)
    webservice部署到服务器报错
  • 原文地址:https://www.cnblogs.com/lz87/p/12258287.html
Copyright © 2011-2022 走看看