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
相关推荐
aqiu11111141 分钟前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)
开发语言·c++·算法
信奥卷王9 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵9 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG11 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z11 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛12 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记12 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序12 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室12 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法