[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;
    }
}

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

时间: 2024-07-30 16:01:36

[LeetCode 955] Delete Columns to Make Sorted II的相关文章

LC 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",&qu

leetcode 944. 删列造序(Delete Columns to Make Sorted)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符. 所余下的字符串行从上往下读形成列. 比如,有 A = ["abcdef", "uvwxyz"],删除索引序列 {0, 2, 3},删除后 A 为["bef", "vyz"], A 的列分别为["b&q

[Swift Weekly Contest 115]LeetCode960. 删列造序 ||| | Delete Columns to Make Sorted III

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 = ["babca",&quo

Weekly Contest 111--------&gt;944. Delete Columns to Make Sorted

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 a string "abcdef" and dele

[Swift Weekly Contest 111]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted

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 a string "abcdef" and dele

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

leetcode第一刷_Remove Duplicates from Sorted Array II

水题. 我之前说过包含至多几个至少几个的问题都比较难,这个题可是让我大脸了.至多可以重复一次,那就重复次数多于两次再计算重复,否则的话像普通的数据一样直接按照重复次数前移就可以了嘛.不过说归说,这种inspace的思想还是有些用处的,数组这种实现方式致命的缺点就是删除或者添加中间的元素代价太大,因为不好把握数据的最终位置.这个题是一种情况,合并两个排序好的数组也是一个例子. class Solution { public: int removeDuplicates(int A[], int n)