LeetCode 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 an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"], and the remaining columns of Aare ["b","v"]["e","y"], and ["f","z"].  (Formally, the c-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]].)

Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order.

Return the minimum possible value of D.length.

题目分析及思路

题目给出一个数组A,包含N个有相同长度的小写字符串。要求得到一个删除索引集合的最小长度,使得删除元素后的A的每一列字母是以非降序的顺序排列。简单来说就是得到所有非降序的排列的数目。

python代码​

class Solution:

    def minDeletionSize(self, A):

        """

        :type A: List[str]

        :rtype: int

        """

        rows = len(A)

        cols = len(A[0])

        count = 0

        for col in range(cols):

            for row in range(1,rows):

                if A[row-1][col]>A[row][col]:

                    count +=1

                    break

        return count

 

posted on 2019-01-07 10:09  锋上磬音  阅读(116)  评论(0编辑  收藏  举报