题目如下:

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

解题思路:这题在【leetcode】944. Delete Columns to Make Sorted 的基础上提升了难度,【leetcode】944. Delete Columns to Make Sorted 只要求了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

【leetcode】955. Delete Columns to Make Sorted II的更多相关文章

  1. 【leetcode】960. 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 cho ...

  2. 【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【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 cho ...

  4. 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 an ...

  5. 【Leetcode_easy】944. Delete Columns to Make Sorted

    problem 944. Delete Columns to Make Sorted 题意:其实题意很简单,但是题目的description给整糊涂啦...直接看题目标题即可理解. solution: ...

  6. 【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...

  7. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  8. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  9. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

随机推荐

  1. case when使用的总结

    https://blog.csdn.net/changxiangyangy/article/details/86718551 前几天,为了给产品分析当前用户数据结构,写sql的时候使用到了case w ...

  2. JavaScript仿淘宝实现放大镜效果的实例

    我们都知道放大镜效果一般都是用于一些商城中的,列如每当我们打开淘宝,天猫等pc端时,看到心仪的物品时,点击图片时,便呈现出放大镜的效果.在没有去理解分析它的原理时,感觉非常的神奇,当真正地去接触,也是 ...

  3. c++简单线程池实现(转)

    线程池,简单来说就是有一堆已经创建好的线程(最大数目一定),初始时他们都处于空闲状态,当有新的任务进来,从线程池中取出一个空闲的线程处理任务,然后当任务处理完成之后,该线程被重新放回到线程池中,供其他 ...

  4. ambari安装hadoop

    前言 hadoop除了社区版之外,还有两个主流的分支,一个是cdh版本,一个是hdp版本,其中ambari是hdp版本的部署工具 1.ambari安装准备 https://docs.hortonwor ...

  5. elasticsearch 6.x 安装search guard

    前言 es之前版本一直无用户验证功能,不过官方有提供一x-pack,但是问题是付费.在es的6.3.2版本中,已经集成了x-pack,虽然es团队已经对x-pack开源,但是在该版本中如果需要使用到安 ...

  6. Tomcat6 只允许指定域名访问,禁用IP地址访问,防止恶意解析

    运维网监控突然同事反应,在百度上搜索其他域名,竟然打开了和我们P2P一模一样的网站,我第一个反应是源代码被盗用了.后来发现,是域名被恶意解析了,解决方法 1.禁止IP地址访问项目 2.只允许指定的域名 ...

  7. 观察者模式Observer

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11409319.html 1. 定义定义对象间的一种一对多的依赖关系.当一个对象的状态发生改变时,所有依 ...

  8. Tomcat是否关闭 maxEntriesLocalHeap

    EHCache does not allow attribute "maxEntriesLocalHeap". 这个错误是由于这个属性不支持2.5以下版本 故更新ehcache版本 ...

  9. java中 抽象类和接口的区别

    一. 什么是抽象类及什么是抽象方法 抽象方法是一种特殊的方法:他只有声明,而没有具体实现,抽象方法的声明格式为: abstract void funName(); 抽象方法必须用 abstract 修 ...

  10. USB编程概念

     RE:USB编程概念 >请你指导我如何学习USB虚拟COM端口PROGRAMM,什么是我们needto有(如硬件和软件)的东西. USB是一个典型的复杂的协议栈,在协议的协议.这是很难完全实现 ...