leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit d728db446cb8f9a7f9bce4c3e580b9ded8210026
parent 4f19154e443722de5388535c3f43646feb615f7c
Author: Jack Sangdahl <jack@pngu.org>
Date:   Fri, 19 Dec 2025 20:08:44 -0700

944. delete columns to make sorted

Diffstat:
Adelete_columns_sorted.c | 29+++++++++++++++++++++++++++++
Adelete_columns_sorted.py | 24++++++++++++++++++++++++
2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/delete_columns_sorted.c b/delete_columns_sorted.c @@ -0,0 +1,29 @@ +// 944. Delete Columns to Make Sorted +#include "leetcode.h" + +/* + * 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. + * you want to delete the columns that are not sorted lexicographically. return + * the number of columns that you will delete. + */ + +int minDeletionSize(char **strs, int strsSize) { + int ans = 0, n = strlen(strs[0]); + for (int j = 0; j < n; j++) + for (int i = 0; i < strsSize - 1; i++) + if (strs[i][j] > strs[i + 1][j]) { + ans++; + break; + } + return ans; +} + +int main() { + char *s1[] = {"cba", "daf", "ghi"}; + char *s2[] = {"a", "b"}; + char *s3[] = {"zyx", "wvu", "tsr"}; + printf("%d\n", minDeletionSize(s1, ARRAY_SIZE(s1))); // expect: 1 + printf("%d\n", minDeletionSize(s2, ARRAY_SIZE(s2))); // expect: 0 + printf("%d\n", minDeletionSize(s3, ARRAY_SIZE(s3))); // expect: 3 +} diff --git a/delete_columns_sorted.py b/delete_columns_sorted.py @@ -0,0 +1,24 @@ +# 944. Delete Columns to Make Sorted + +""" +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. +you want to delete the columns that are not sorted lexicographically. return +the number of columns that you will delete. +""" + + +class Solution(object): + def minDeletionSize(self, strs): + """ + :type strs: List[str] + :rtype: int + """ + return sum(any(a > b for a, b in zip(i, i[1:])) for i in zip(*strs)) + + +if __name__ == "__main__": + obj = Solution() + print(obj.minDeletionSize(strs=["cba", "daf", "ghi"])) + print(obj.minDeletionSize(strs=["a", "b"])) + print(obj.minDeletionSize(strs=["zyx", "wvu", "tsr"]))