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;
        }
    }
  • 相关阅读:
    第19 章 : 调度器的调度流程和算法介绍
    第18 章 : Kubernetes 调度和资源管理
    关于一次配合开发工作而产生的服务器内核参数问题(Android 网络问题)
    第17 章 : 深入理解 etcd:etcd 性能优化实践
    第16 章 : 深入理解 etcd:基于原理解析
    第15 章 : 深入解析 Linux 容器
    第14 章 : Kubernetes Service讲解
    第13 章 : Kubernetes 网络概念及策略控制
    第12 章 : 可观测性-监控与日志
    第11 章 : 可观测性:你的应用健康吗?(liveness和readiness)
  • 原文地址:https://www.cnblogs.com/lz87/p/12258287.html
Copyright © 2011-2022 走看看