Skip to content

Latest commit

 

History

History
52 lines (43 loc) · 1.72 KB

풀이_LC944.md

File metadata and controls

52 lines (43 loc) · 1.72 KB

⚽️ 944. Delete Columns to Make Sorted

  • Date : 2022.02.27(월)
  • Time : 30분

Problem

  • 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
    
  • ou 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.

Constraints

  • n == strs.length
  • 1 <= n <= 100
  • 1 <= strs[i].length <= 1000
  • strs[i] consists of lowercase English letters.

Example

  • Input: strs = ["cba","daf","ghi"]
  • Output: 1
  • Explanation: The grid looks as follows:
    cba
    daf
    ghi
    
    Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.

풀이

    def minDeletionSize(self, strs: List[str]) -> int:
        delete = 0 
        for i in range(len(strs[0])):
            temp_col_str = ""
            for str in strs:
                if temp_col_str != "" and temp_col_str[-1] > str[i]:
                   delete += 1
                   break
                else:
                   temp_col_str += str[i]
            
        return delete

: 이 문제는 각 열끼리 비교해주는 문제이다. 각 열끼리 비교를 통해서 정렬이 잘 되어 있다면 통과. 아니라면 그 열의 위치를 return 해주면 된다. 처음에 비교할 값은 맨 처음 행의 n열로 정해준다. 여기서 만약에 마지막열까지 통과라면 delete 에 +1을 해준다.