944. Delete Columns to Make Sorted

https://leetcode.com/problems/delete-columns-to-make-sorted/

Brutal force, intuitive solution:

class Solution:
    def minDeletionSize(self, A: List[str]) -> int:
        res = 0
        m = max([len(row) for row in A])
        cols = [""]*m
        for row in A:
            for j in range(m):
                if j < len(row):
                    cols[j] += row[j]
        #print(cols)
        for col in cols:
            if "".join(sorted(col)) != col:
                res+=1
        return res
        

同时我们意识到,matrix的A的columns可以表示为, 虽然这里的A是一个list

zip(*A)
class Solution:
    def minDeletionSize(self, A: List[str]) -> int:
        res = 0
        for col in zip(*A):
            if sorted(col) != list(col):
                res+=1
        return res

Last updated

Was this helpful?