955. Delete Columns to Make Sorted II

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

class Solution:
    def minDeletionSize(self, A: List[str]) -> int:
        def isSort(rows):
            for i in range(len(rows)-1):
                if rows[i] > rows[i+1]:
                    return False
            return True
        res = 0
        rows = [""] * len(A)
        for col in zip(*A):
            curr = rows[:]
            for j in range(len(A)):
                curr[j]+= col[j]
            if isSort(curr):
                rows = curr
            else:
                res+=1
        return res
                
        
        

Last updated

Was this helpful?