题目如下:

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 = ["babca","bbazb"] and deletion indices {0, 1, 4}, then the final array after deletions is ["bc","az"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has every element (row) in lexicographic order.

For clarity, A[0] is in lexicographic order (ie. A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]), A[1] is in lexicographic order (ie. A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]), and so on.

Return the minimum possible value of D.length.

Example 1:

Input: ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is A = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. A[0][0] <= A[0][1] and A[1][0] <= A[1][1]).
Note that A[0] > A[1] - the array A isn't necessarily in lexicographic order.

Example 2:

Input: ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.

Example 3:

Input: ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.

Note:

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

解题思路:本题可以采用动态规划的方法。记dp[i][0] = v表示不删除第i个元素时,使得0~i子区间有序需要删除掉v个字符,dp[i][1] = v表示删除第i个元素时,使得0~i子区间有序需要删除掉v个字符。先看第种情况,因为对第i个元素删除操作,所以其值完全和dp[i-1]有关,有dp[i][1] = min(dp[i-1][0],dp[i-1][1]) + 1,取第i个元素删除或者不删除时候的较小值;而如果第i个元素保留,那么我们只需要找出离i最近的保留的元素j,使得Input 中每一个元素 item 都需要满足 item[i] > item[j],这样的j可能不存在或者有多个,找出满足 dp[i][0] = min(dp[i][0],dp[j][0] + (i-j-1)) 最小的即可,如果没有这样的j存在,令dp[i][0] = i。最后的结果为 dp[-1][0]和dp[-1][1]中的较小值。

代码如下:

class Solution(object):
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
dp = [[float('inf')] * 2 for _ in A[0]]
dp[0][0] = 0 # 0 : keep; 1:delete
dp[0][1] = 1 for i in range(1,len(A[0])):
dp[i][1] = min(dp[i-1][0],dp[i-1][1]) + 1
dp[i][0] = i
for j in range(i):
flag = True
for k in range(len(A)):
if A[k][i] < A[k][j]:
flag = False
break
if flag:
dp[i][0] = min(dp[i][0],dp[j][0] + (i-j-1))
return min(dp[-1])

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

  1. 【leetcode】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 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. 【Leetcode_easy】944. Delete Columns to Make Sorted

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

  5. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

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

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

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

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

  8. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  9. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

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

随机推荐

  1. 【Python开发】matplotlib绘图不显示问题解决plt.show()

    最近在看<Python数据分析>这本书,而自己写代码一直用的是Pycharm,在练习的时候就碰到了plot()绘图不能显示出来的问题.网上翻了一下找到知乎上一篇回答,试了一下好像不行,而且 ...

  2. the property “***” on could not be set to a null value

    在建立EF框架的时候,创建实体时由于部分数据库类型和.net类型不同,比如int类型,在数据库中是可空类型,而.net中是不允许的,所以创建实体的时候,数据库的int类型对应的实体类型应该为int?类 ...

  3. luoguP2822-组合数问题(基础DP)

    题目链接:https://www.luogu.org/problemnew/show/P2822 题意:输入T和k,有T组询问.每组询问输入n.m,求C(i,j)能模k的个数(0<=i<= ...

  4. 第一次把mysql装进docker里碰到的各种问题

    最近电脑经常关机要关好长时间,老是需要长按电源键强行关机.也不知道是怎么回事. 后来查看关机时的日志,发现是mysql停不掉.这可闹心了!怎么办?上网搜了搜也没有找到什么好的解决办法.总不能每次关机都 ...

  5. Restful Api调用工具类

    RestfulHttpClient.java package pres.lnk.utils; import com.fasterxml.jackson.databind.ObjectMapper; i ...

  6. 【Java面试题】解释内存中的栈(stack)、堆(heap)和静态存储区的用法

    Java面试题:解释内存中的栈(stack).堆(heap)和静态存储区的用法 堆区: 专门用来保存对象的实例(new 创建的对象和数组),实际上也只是保存对象实例的属性值,属性的类型和对象本身的类型 ...

  7. Uncaught SyntaxError: Unexpected identifier

    $.ajax({ //请求头 type:"POST", contentType:"application/x-www-form-urlencoded", url ...

  8. sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrect string value: '\\xE6\\xB1\\x89\\xE8\\xAF\\xAD...' for column 'className' at row 1") [SQL: INSERT INTO classmessage (`classId

    sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, "Incorrect string value: '\\xE ...

  9. javascript--获取一个页面各个标签的数量

    获取一个页面各个标签的数量 document.getElementsByTagName('*')--获取所有的标签. var obj = document.getElementsByTagName(' ...

  10. IDEA等全家桶设置Ctrl+滚轮调整字体大小

    File→Settings→General,勾选Change font size... 保存.