【leetcode】960. Delete Columns to Make Sorted III
题目如下:
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 = ["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 <= A.length <= 100
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的更多相关文章
- 【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 ...
- 【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 ...
- 【Leetcode_easy】944. Delete Columns to Make Sorted
problem 944. Delete Columns to Make Sorted 题意:其实题意很简单,但是题目的description给整糊涂啦...直接看题目标题即可理解. solution: ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【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 ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
随机推荐
- Oracle 多行拼接函数 :[Err] ORA-00904: "WM_CONCAT": 标识符无效
SELECT LISTAGG(GOODSNAME,';') WITHIN GROUP (ORDER BY GOODSNAME) AS sd WM_CONCAT() 方法被替代成:LISTAGG(GO ...
- Codeforces 817+818(A~C)
(点击题目即可查看原题) 817A Treasure Hunt 题意:给出起点和终点,每次移动只能从 (a,b)移动至(a+x,b+y) , (a+x,b-y) , (a-x,b+y) , (a-x, ...
- 关于Windows10内存随时间不断升高问题
问题描述 电脑买了10个月了,头半年的运行内存都是正常的,基本不会超过60%,但是最近几个月发现自己电脑的运行内存会随时间不断地升高,关机后重启也无法解决这个问题QAQ 常见的症状为一开机,点开任务管 ...
- php 内存原理
1.内存结构: 栈区:保存变量名,对CPU,读写速度很快 堆区:保存复杂的数据结构,想对象,数组,复杂的字符串等. 数据段:数据段分为数据段全局区(简单的数据,整型和布尔类型)和数据段静态区(存储静态 ...
- excel批量修改文件名称
步骤: 1. 然后将标准号所在的列复制三份到旁边相邻的空白列 2. 使用方方格子让三列中最左面的列只保存如下格式的数据: DB37/T或DB37/ 3. 使用方方格子让三列中中间的列的只保存如下格式的 ...
- EM 算法(三)-GMM
高斯混合模型 混合模型,顾名思义就是几个概率分布密度混合在一起,而高斯混合模型是最常见的混合模型: GMM,全称 Gaussian Mixture Model,中文名高斯混合模型,也就是由多个高斯分布 ...
- angular 4+中关于父子组件传值的示例
home.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-hom ...
- 关于redis的几件小事(六)redis的持久化
1.redis持久化的意义 redis持久化的意义,在于 故障恢复 . 如果没有对数据进行持久化,那么如果redis遇到灾难性的故障,就会丢失所有的数据. 如果通过redis的持久化机制将数据持久化到 ...
- 详解Wox
Wox 是一款国产开源免费的软件快捷启动工具,它可以快速搜索并打开你电脑上的程序.文件.或是查词翻译.网站查找等其他操作,同时还支持插件安装. Tips: 如果你需要用到文件的快速搜索.打开功能,需要 ...
- 使用sublimeserver启动本地服务器进行调试
最近在做前后端分离的项目,访问后台接口的时候会产生跨域问题,修改了相关配置解决了跨域问题,但是配置中只对开发环境进行了设置,没有设置生产环境,为了验证生产环境确实无法访问后台接口遂npm run bu ...