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
相关推荐
嗷嗷哦润橘_9 分钟前
从萝卜纸巾猫到桌游:“蒸蚌大开门”的设计平衡之旅
人工智能·算法·游戏·概率论·桌游
TracyCoder12328 分钟前
Java String:从内存模型到不可变设计
java·算法·string
我是大咖38 分钟前
二维数组与数组指针
java·数据结构·算法
筵陌1 小时前
算法:动态规划
算法·动态规划
大江东去浪淘尽千古风流人物1 小时前
【DSP】xiBoxFilter_3x3_U8 dsp VS cmodel
linux·运维·人工智能·算法·vr
zhuqiyua1 小时前
【无标题】
算法
Xの哲學2 小时前
Linux Tasklet 深度剖析: 从设计思想到底层实现
linux·网络·算法·架构·边缘计算
Imxyk2 小时前
力扣:1553. 吃掉 N 个橘子的最少天数(记忆化搜索,Dijkstra解法)
算法
爱编码的傅同学2 小时前
【今日算法】Leetcode 581.最短无序连续子数组 和 42.接雨水
数据结构·算法·leetcode
Σίσυφος19002 小时前
线性与非线性 、齐次非齐次
算法