Problem statement

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

Solution

Just check for each column if it is sorted.

Complexity

It is O(mn), because for sorted array function sorted will work in linear time.

Code

class Solution:
    def minDeletionSize(self, strs):
        n, m, ans = len(strs[0]), len(strs), 0
        for i in range(n):
            col = [strs[x][i] for x in range(m)] 
            ans += col == sorted(col)
            
        return n - ans