Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 944. Delete Columns to Make Sorted

Rain Hu

944. Delete Columns to Make Sorted


一、題目

You are given an array of n string strs, all of the same length.
The string s can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae" can be arranged as :

abc
bce
cae

You want to delete the columns that are not sorted lexicographically. In the aove 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.

Example 1:

Example 2:

Example 3:

Constraints:


二、分析

三、解題

1. String

int minDeletionSize(vector<string>& strs) {
    int m = strs.size(), n = strs[0].size();
    int cnt = 0;
    for (int col = 0; col < n; ++col) {
        for (int row = 1; row < m; ++row) {
            if (strs[row-1][col] > strs[row][col]) {
                cnt++;
                break;
            }
        }
    }
    return cnt;
}

回目錄 Catalog


Share this post on:

Previous
[Algo] 1-9. Algorithm
Next
[LeetCode] 2522. Partition String Into Substrings With Values at Most K