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
相关推荐
阿Y加油吧7 小时前
力扣打卡——搜索二维矩阵、相交链表
线性代数·leetcode·矩阵
普贤莲花7 小时前
【2026年第11周---写于20260322】
程序人生·算法·leetcode
小白自救计划7 小时前
力扣知识点杂集
算法·leetcode·哈希算法
阿贵---7 小时前
分布式系统C++实现
开发语言·c++·算法
不染尘.7 小时前
最短路径之Bellman-Ford算法
开发语言·数据结构·c++·算法·图论
big_rabbit05027 小时前
JVM堆内存查看命令
java·linux·算法
m0_662577977 小时前
C++中的RAII技术深入
开发语言·c++·算法
旖-旎7 小时前
二分查找(点名)(8)
c++·算法·二分查找·力扣
承渊政道7 小时前
【优选算法】(实战体验滑动窗口的奇妙之旅)
c语言·c++·笔记·学习·算法·leetcode·visual studio
lemonth7 小时前
图形推理----
人工智能·算法·机器学习