Weekly Contest 111-------->944. Delete Columns to Make Sorted

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have a string "abcdef" and deletion indices {0, 2, 3}, then the final string after deletion is "bef".

Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order.

Formally, the c-th column is [A[0][c], A[1][c], ..., A[A.length-1][c]]

Return the minimum possible value of D.length.

Example 1:

Input: ["cba","daf","ghi"]
Output: 1

Example 2:

Input: ["a","b"]
Output: 0

Example 3:

Input: ["zyx","wvu","tsr"]
Output: 3

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 1000

Approach #1:

class Solution {
public:
    int minDeletionSize(vector<string>& A) {
        int size = A.size();
        int len = A[0].size();
        int ans = 0;
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < size-1; ++j) {
                if (A[j][i] > A[j+1][i]) {
                    ans++;
                    break;
                }
            }
        }
        return ans;
    }
};

  

原文地址:https://www.cnblogs.com/ruruozhenhao/p/9977519.html

时间: 2024-08-30 18:06:49

Weekly Contest 111-------->944. Delete Columns to Make Sorted的相关文章

LC 955. Delete Columns to Make Sorted II

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef",&qu

[Swift Weekly Contest 111]LeetCode944. 删除列以使之有序 | Delete Columns to Make Sorted

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have a string "abcdef" and dele

[Swift Weekly Contest 115]LeetCode960. 删列造序 ||| | Delete Columns to Make Sorted III

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["babca",&quo

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

leetcode 944. 删列造序(Delete Columns to Make Sorted)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符. 所余下的字符串行从上往下读形成列. 比如,有 A = ["abcdef", "uvwxyz"],删除索引序列 {0, 2, 3},删除后 A 为["bef", "vyz"], A 的列分别为["b&q

[LeetCode 955] Delete Columns to Make Sorted II

We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices. For example, if we have an array A = ["abcdef",&qu

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

LeetCode之Weekly Contest 93

第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . 在 22 的二进制表示中,有三个 1,组成两对连续的 1 . 第一对连续的 1 中,两个 1 之间的距离为 2 . 第二对连续的 1 中,两个 1 之间的距离为 1 . 答案取两个距离之中最大的,也就是 2 . 示例 2: 输入:5 输出:2 解释: 5 的二进制是 0

LeetCode之Weekly Contest 101

前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不出来的直接放弃. 第一题:问题 问题:900. RLE 迭代器 编写一个遍历游程编码序列的迭代器. 迭代器由 RLEIterator(int[] A) 初始化,其中 A 是某个序列的游程编码.更具体地,对于所有偶数i,A[i] 告诉我们在序列中重复非负整数值 A[i + 1] 的次数. 迭代器支持一