Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Last active December 21, 2025 23:01
Show Gist options
  • Select an option

  • Save Ifihan/9b9a5b3c34819ccf8cdfe87cf68a84c4 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/9b9a5b3c34819ccf8cdfe87cf68a84c4 to your computer and use it in GitHub Desktop.
Delete Columns to Make Sorted

Question

Approach

I iterate column by column. For each column, I compare characters from top to bottom. If I find any row where the character is smaller than the one above it, the column is not lexicographically sorted, so I count it as a deletion and move to the next column.

Implementation

class Solution:
    def minDeletionSize(self, strs: List[str]) -> int:
        rows, cols = len(strs), len(strs[0])
        deletions = 0

        for c in range(cols):
            for r in range(1, rows):
                if strs[r][c] < strs[r - 1][c]:
                    deletions += 1
                    break

        return deletions

Complexities

  • Time: O(n × m)
  • Space: O(1)
image
@Elvmeen
Copy link

Elvmeen commented Dec 21, 2025

This solution works only because it blindly relies on problem constraints. It makes unchecked assumptions about non-empty input and equal-length strings, offers no defensive handling, and adds nothing beyond the most obvious brute-force approach. The explanation is minimal and lacks any justification beyond restating the code.

@Ifihan
Copy link
Author

Ifihan commented Dec 21, 2025

Yes, it's a brute force solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment