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 小时前
Matlab系列(006) 一利用matlab保存txt文件和读取txt文件
开发语言·算法·matlab
Dev7z2 小时前
基于Matlab实现GRACE卫星重力数据的全球水储量变化估算与分析
人工智能·算法·matlab
爱喝热水的呀哈喽3 小时前
11题目汇总
算法
三斗米3 小时前
Transformer入门:一文读懂《Attention Is All You Need》
算法·架构
Swift社区3 小时前
LeetCode 458 - 可怜的小猪
算法·leetcode·职场和发展
AI科技星4 小时前
宇宙的像素:真空中一点如何编码无限星光
数据结构·人工智能·算法·机器学习·重构
程芯带你刷C语言简单算法题4 小时前
Day37~求组合数
c语言·开发语言·学习·算法·c
程序员-周李斌4 小时前
transmittable-thread-local[线程池跨线程值传递]
java·开发语言·算法·散列表
Flash.kkl4 小时前
优先算法专题十七——多源BFS
算法·宽度优先