题目如下:
We are given an array
A
ofN
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 <= A.length <= 100
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的更多相关文章
-
【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 ...
-
【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
-
【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 ...
-
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 ...
-
【Leetcode_easy】944. Delete Columns to Make Sorted
problem 944. Delete Columns to Make Sorted 题意:其实题意很简单,但是题目的description给整糊涂啦...直接看题目标题即可理解. solution: ...
-
【LeetCode】768. Max Chunks To Make Sorted II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-chun ...
-
【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
-
【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
-
【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
随机推荐
-
我自己做的网站终于上线啦,用tornado做的,求围观 www.yustock.live
跟股票相关的一个小网站~ www.yustock.live
-
配置windows下驱动开发环境
1.下载安装Visual Studio 2008. 2.下载安装WDK 下载地址(点此下载) 建议完全安装,为了便于配置,作者安装到C盘.(C:/WinDDK) 3. ddkwizards(ddkwi ...
-
find the majority element
Runtime: O(n) — Moore voting algorithm: We maintain a current candidate and a counter initialized to ...
-
android学习之-Theme和Style
android学习之-Theme和Style 分类: android 2013-10-11 15:01 960人阅读 评论(0) 收藏 举报 android style和theme的使用. style ...
-
laravel 5.5 接入蚂蚁金服官方SDK(支付宝APP支付为例)开发步骤
一.创建应用及配置 首先需要到蚂蚁金服开放平台(open.alipay.com)注册应用,获取应用id(APP_ID),并且配置应用,主要是签约应用,这个需要审核,一般2-5个工作日,审核通过后,去生 ...
-
模块机制 之commonJs、node模块 、AMD、CMD
在其他高级语言中,都有模块中这个概念,比如java的类文件,PHP有include何require机制,JS一开始就没有模块这个概念,起初,js通过<script>标签引入代码的方式显得杂 ...
-
005-CSS让页脚始终在底部不论页面内容多少
让页脚始终在页面底部,不论页面内容是多或者少页脚始终在页面底部. 方案一: <!DOCTYPE html> <html> <head> <meta chars ...
-
【Spring源码解读】bean标签中的属性(一)你可能还不够了解的 scope 属性
scope 属性说明 在spring中,在xml中定义bean时,scope属性是用来声明bean的作用域的.对于这个属性,你也许已经很熟悉了,singleton和prototype信手捏来,甚至还能 ...
-
XPATH语法(一)
Xpath简介 XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言的子集)文档中某部分位置的语言.XPath基于XML的树状结构,有不同类型的节点,包括元素节点,属性节点和文本节点 ...
-
Sql Server 修改表所属用户
Sql Server 修改表所属用户 exec sp_changeobjectowner 'tablename','dbo' tablename--所要修改的表明 dbo--是表所属的用户,默认是db ...