Skip to content

Latest commit

 

History

History
34 lines (27 loc) · 878 Bytes

2023-01-03.md

File metadata and controls

34 lines (27 loc) · 878 Bytes

题目

  1. Delete Columns to Make Sorted

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:

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.

思路

代码

var minDeletionSize = function(strs) {
  let count = 0
  for (let col = 0; col < strs[0].length; col++) {
    for (let row = 1; row < strs.length; row++) {
      if (strs[row][col] < strs[row-1][col]) {
        count++
        break
      }
    }
  }
  return count
}