【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/delete-columns-to-make-sorted/description/
题目描述
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 a string "abcdef" and deletion indices {0, 2, 3}, then the final string after deletion is “bef”.
Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order.
Formally, the c-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]]
Return the minimum possible value of D.length.
Example 1:
Input: ["cba","daf","ghi"]
Output: 1
Example 2:
Input: ["a","b"]
Output: 0
Example 3:
Input: ["zyx","wvu","tsr"]
Output: 3
Note:
1 <= A.length <= 1001 <= A[i].length <= 1000
题目大意
有一个数组A,其中它的每个元素都是等长度的字符串。现在求最短的要删除的切片的长度,使得做完操作之后,数组中剩下的相同列是递增的。
解题方法
又是乍一看很难的题目,其实很简单的,直接暴力解决就好了。
主要是思路:如果一个列的元素已经是递增的,那么我们一定不能把这个列删除掉。如果删除掉某一列,那么其他的列将不受到任何影响。
所以,基于上面两个原则,我们可以直接对所有的列进行遍历,也就是说取得所有的列,然后判断这个列是不是递增的,如果不是递增的话,就删除掉。统计要删除掉多少个列即可。而判断某个列是不是递增的最简单方法就是排序之后,然后看是不是和之前的相等。
时间复杂度是O(N*(MlogM)),空间复杂度是O(M)。N是每个字符串长度,M是数组长度。还好给的区间很小,直接能过的。
class Solution:
def minDeletionSize(self, A):
"""
:type A: List[str]
:rtype: int
"""
res = 0
N = len(A[0])
for i in range(N):
col = [a[i] for a in A]
if col != sorted(col):
res += 1
return res
日期
2018 年 11 月 18 日 —— 出去玩了一天,腿都要废了
【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)的更多相关文章
- 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 choo ...
- Leetcode 944. Delete Columns to Make Sorted
class Solution: def minDeletionSize(self, A: List[str]) -> int: ans = 0 for j in range(len(A[0])) ...
- 【Leetcode_easy】944. Delete Columns to Make Sorted
problem 944. Delete Columns to Make Sorted 题意:其实题意很简单,但是题目的description给整糊涂啦...直接看题目标题即可理解. solution: ...
- 【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】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
随机推荐
- Excel-实现选择性粘贴(粘贴公式为文本)自动化,不想手动
10.选择性粘贴(粘贴公式为文本)自动化,不想手动: (1)参考:https://jingyan.baidu.com/article/20b68a88a8bf55796cec62a3.html (2) ...
- Perl 常用的小细节总结
1.命令行:perl -c perl.pl #用来检验Perl脚本有没有错误: 2.vi perl.pl打开脚本,ESC+:set nu 回车,给每行加上行号:
- 设置administrator账号密码
设置administrator账号密码: 打开:附件->运行 输入:lusrmgr.msc 在里面的用户里修改administrator密码
- Visual Studio Code常用操作整理
Live Server插件可以在保存html文件后实时地刷新页面 在html文件中键入"! +Tap"会生成一个html模板 保存文件:Ctrl+S 文件跳转:Ctrl+P 文件内 ...
- The Ultimate Guide to Buying A New Camera
[photographyconcentrate] 六级/考研单词: embark, thrill, excite, intimidate, accessory, comprehensive, timi ...
- c学习 - 第五章:选择结构程序设计
5.2 关系运算符与逻辑运算符 !(非) ^ 高 算术运算符 | 关系运算符 | &&和 || | 赋值运算符 | 低
- vmware使用nat连接配置
一.首先查看自己的虚拟机服务有没有开启,选择电脑里面的服务查看: 1.计算机点击右键选择管理 2.进入管理选择VM开头的服务如果没有开启的话就右键开启 二.虚拟机服务开启后就查看本地网络虚拟机的网 ...
- 转Android Canvas和Paint基本使用
Android Canvas和Paint基本使用 这篇文章主要介绍下画笔Paint和画布Canvas的基本使用 1.Paint 创建对象Paint mPaint = new Paint(); 常 ...
- oracle keep
语法: min | max(column1) keep (dense_rank first | last order by column2) over (partion by column3); -- ...
- BlockingQueue的基本原理
1. 前言 BlockingQueue即阻塞队列,它算是一种将ReentrantLock用得非常精彩的一种表现,依据它的基本原理,我们可以实现Web中的长连接聊天功能,当然其最常用的还是用于实现生产者 ...