Leetcode 944. Delete Columns to Make Sorted

Problem

You are given an array of n strings strs, all of the same length.

The strings can be arranged such that there is one on each line, making a grid.

  • For example, strs = "abc", "bce", "cae" can be arranged as follows:

    abc
    bce
    cae

You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted, while column 1 ('b', 'c', 'a') is not, so you would delete column 1.

Return the number of columns that you will delete.

Algorithm

To count the number of columns not sorted in ascending order, a straightforward tally is ok.

Code

python3 复制代码
class Solution:
    def minDeletionSize(self, strs: List[str]) -> int:
        rows, cols, cnts = len(strs), len(strs[0]), 0
        for c in range(cols):
            for r in range(rows-1):
                if strs[r][c] > strs[r+1][c]:
                    cnts += 1
                    break
        return cnts
相关推荐
菜菜的顾清寒2 分钟前
力扣HOT100(42)链表-随机链表的复制
算法·leetcode·链表
lqqjuly9 分钟前
模型剪枝与稀疏化:理论、算法与可运行实现
人工智能·算法·剪枝
逻辑君26 分钟前
Foresight研究报告【20260011】
人工智能·线性代数·算法·矩阵
珊瑚里的鱼26 分钟前
【动态规划】不同路径Ⅱ
算法·动态规划
适应规律1 小时前
【无标题】
人工智能·python·算法
蒟蒻的贤1 小时前
关于文法G2算符优先分析的一个坑
算法
变量未定义~2 小时前
单调栈、单调队列(模板)、子矩阵、连通块中点的数量、堆箱子(4星)
算法
通信小呆呆2 小时前
Vandermonde结构及其快速算法详解
线性代数·算法
云泽8083 小时前
笔试算法 - 链表篇(一):移除、反转、合并、回文判断全解析
数据结构·c++·算法·链表