zoukankan      html  css  js  c++  java
  • 【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 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])
  • 相关阅读:
    100个经典的动态规划方程
    poj 2105 IP Address
    codeforces 299 A. Ksusha and Array
    codeforces 305 C. Ivan and Powers of Two
    性能测试实战 | 修改 JMeter 源码,定制化聚合压测报告
    Pytest 测试框架训练营,测试大咖带你搞定自动化+接口测试实战 | 限时免费报名
    MTSC 测试开开发大会(深圳站),报名立减 100 元!
    测试人面试 BAT 大厂之前,需要做好哪些准备?
    iOS 自动化测试踩坑(二):Appium 架构原理、环境命令、定位方式
    【北京】美团打车招聘职位
  • 原文地址:https://www.cnblogs.com/seyjs/p/11791572.html
Copyright © 2011-2022 走看看