力扣每日一题2022-05-12删列造序

力扣每日一题2022-05-12删列造序,第1张

删列造序
    • 题目描述
    • 思路
      • 模拟
        • Python实现
        • Java实现


题目描述

删列造序


思路 模拟

根据题意模拟即可。对于第j列,只需要判断所有相邻字符判断strs[i-1][j] <= strs[i][j]即可。

Python实现
class Solution:
    def minDeletionSize(self, strs: List[str]) -> int:
        return sum(list(column) != sorted(column) for column in zip(*strs))
Java实现
class Solution {
    public int minDeletionSize(String[] strs) {
        int n = strs.length, m = strs[0].length();
        int ans = 0;
        for (int j = 0; j < m; ++j) {
            for (int i = 1; i < n; ++i) {
                if (strs[i-1].charAt(j) > strs[i].charAt(j)) {
                    ans++;
                    break;
                }
            }
        }
        return ans;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/917468.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存