题目如下:

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

最新文章

  1. 51nod1102(数塔)
  2. 获取html 中的内容 将前台的数据获取到后台
  3. eclise 部署web工程报 There are no resources that can be added or removed from the server.
  4. [推荐] BC/Beyond Compare(差异比较软件)
  5. AcceptEx编辑
  6. http://www.jobui.com/mianshiti/it/java/6782/
  7. 初识 Asp.Net内置对象之Request对象
  8. javascript——面向对象程序设计(4)
  9. python encode和decode函数说明【转载】
  10. Unix/Linux环境C编程入门教程(12) openSUSECCPP以及Linux内核驱动开发环境搭建
  11. Android的BUG(二) - SurfaceTexture中的野指针
  12. rgba兼容性处理
  13. SAP的这三款CRM解决方案,您能区分清楚么
  14. 管理xcode插件
  15. shell练习题6
  16. TODO 动态执行appium代码,便于修改和调试
  17. 《SQL 基础教程》第四章:数据更新
  18. 性能测试&#160;Apache参数配置与性能调优
  19. ALGO-27_蓝桥杯_算法训练_FBI树(树,递归)
  20. AndroidManifest.xml中android:configChanges的简介

热门文章

  1. 【leetcode】1004. Max Consecutive Ones III
  2. Red Hat Enterprise Linux 7.x新特性
  3. python selenium模拟登陆163邮箱。
  4. 85、使用TFLearn实现iris数据集的分类
  5. mybatis PageBounds应用分页
  6. Redis 5.0.7 讲解,单机、集群模式搭建
  7. PHP面试 AJAX基础内容
  8. Eclipes 安装windowbuilding
  9. HBase优化——读写优化
  10. js中继承的实现,原型链的知识点归纳,借用构造函数,组合继承(伪经典继承)