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

目录

  • 题目描述:
  • 示例 1:
  • 示例 2:
  • 示例 3:
  • 解法:

题目描述:

给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等。

选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符。 所余下的字符串行从上往下读形成列。

比如,有 A = ["abcdef", "uvwxyz"],删除索引序列 {0, 2, 3},删除后 A["bef", "vyz"], A 的列分别为["b","v"], ["e","y"], ["f","z"]。(形式上,第 n 列为 [A[0][n], A[1][n], ..., A[A.length-1][n]])。

假设,我们选择了一组删除索引 D,那么在执行删除操作之后,A 中所剩余的每一列都必须是 非降序 排列的,然后请你返回 D.length 的最小可能值。

示例 1:

输入:["cba", "daf", "ghi"]
输出:1
解释:
    当选择 D = {1},删除后 A 的列为:["c","d","g"] 和 ["a","f","i"],均为非降序排列。
    若选择 D = {},那么 A 的列 ["b","a","h"] 就不是非降序排列了。

示例 2:

输入:["a", "b"]
输出:0
解释:D = {}

示例 3:

输入:["zyx", "wvu", "tsr"]
输出:3
解释:D = {0, 1, 2}

提示:

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

解法:

class Solution {
public:
    int minDeletionSize(vector<string>& A) {
        int m = A.size();
        int n = A[0].size();
        int res = 0;
        for(int i = 0; i < n; i++){
            bool del = false;
            for(int j = 1; j < m; j++){
                if(A[j][i] < A[j-1][i]){
                    del = true;
                    break;
                }
            }
            if(del){
                res++;
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10670371.html

时间: 2024-11-05 23:33:49

leetcode 944. 删列造序(Delete Columns to Make Sorted)的相关文章

[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

944. 删列造序

944. 删列造序 描述: 给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 删除 操作的定义是:选出一组要删掉的列,删去 A 中对应列中的所有字符,形式上,第 n 列为 [A[0][n], A[1][n], ..., A[A.length-1][n]]). 比如,有 A = ["abcdef", "uvwxyz"], 要删掉的列为 {0, 2, 3},删除后 A 为["bef", "vyz"], A 的列

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

[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

[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

Weekly Contest 111--------&gt;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 dele

[leetcode]重建二叉树(先序和终须) 中序遍和后续

分割后长度相等,就是参数麻烦,p,先序的起始点, ib,ie 终须的结束和开始. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public TreeNode bu

[LeetCode]题解(python):081 - Search in Rotated Sorted Array II

题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va