博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】955. Delete Columns to Make Sorted II
阅读量:6990 次
发布时间:2019-06-27

本文共 2084 字,大约阅读时间需要 6 分钟。

题目如下:

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: 1Explanation: 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: 0Explanation: 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: 3Explanation: We have to delete every column.

 

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100

解题思路:这题在 的基础上提升了难度, 只要求了A中所有字符串在同一索引的字符是升序的,而本题是要求字符串本身是升序的。我的解法是对A中所有字符串进行前缀比较,前缀的区间起始是0,重点是end ( 0 <= end < 字符串长度)。如果前缀比较中发现不满足升序条件,那么删除掉end位置的所有元素;否则end += 1,继续往后比较。

代码如下:

class Solution(object):    def minDeletionSize(self, A):        """        :type A: List[str]        :rtype: int        """        end = 0        res = 0        while end < len(A[0]):            for i in range(len(A)-1):                #print A[i][:end+1],A[i+1][:end+1]                if A[i][:end+1] > A[i+1][:end+1]:                    res += 1                    #delete end + 1                    for j in range(len(A)):                        A[j] = A[j][:end] + A[j][end+1:]                    end -= 1                    break                else:                    continue            end += 1        return res

 

转载于:https://www.cnblogs.com/seyjs/p/10092552.html

你可能感兴趣的文章
London2012同步时间新语
查看>>
Android如何获取多媒体文件信息
查看>>
端口列表详解
查看>>
ecshop 用户中心
查看>>
浅谈MySql的存储引擎(表类型)
查看>>
第三方控件DevExpress中ASPxNavBar1用法
查看>>
Spring.net、NHibernate相关文章导航
查看>>
Android中Application设置全局变量以及传值
查看>>
每日英语:The Rise Of The Female Investor
查看>>
黑马程序员-JAVA基础-基本数据类型对象包装类
查看>>
Gzip Zlib PNG 压缩算法,源码详解 - swo2006 - C++博客
查看>>
Console-算法[foreach,if]-一输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数...
查看>>
[原][问题解决]Openstck云平台虚拟机无法连接问题解决
查看>>
状态控件ios 中滑块、开关、分段控件、操作表和警告的常用函数
查看>>
分享非常漂亮的WPF界面框架源码及其实现原理
查看>>
如何获取ResultSet的行数和列数(转)
查看>>
绑定列ORA-24816: 在实际的 LONG 或 LOB 列之后提供了扩展的非 LONG 绑定数据
查看>>
Mobile Web调试工具Weinre
查看>>
Android巴士转发
查看>>
未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。
查看>>