From 3bb3d177c91a8d45a25ce951343cfa5f1b462369 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 6 Jul 2024 19:29:14 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0944 No.0944.Delete Columns to Make Sorted --- .../README.md | 60 +++++++++++++------ .../README_EN.md | 60 +++++++++++++------ .../Solution.cpp | 15 +++-- .../Solution.go | 5 +- .../Solution.rs | 12 ++-- .../Solution.ts | 13 ++++ 6 files changed, 112 insertions(+), 53 deletions(-) create mode 100644 solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.ts diff --git a/solution/0900-0999/0944.Delete Columns to Make Sorted/README.md b/solution/0900-0999/0944.Delete Columns to Make Sorted/README.md index 505b8e25c52da..f40ef75ad4166 100644 --- a/solution/0900-0999/0944.Delete Columns to Make Sorted/README.md +++ b/solution/0900-0999/0944.Delete Columns to Make Sorted/README.md @@ -84,7 +84,15 @@ cae -### 方法一 +### 方法一:逐列比较 + +我们记字符串数组 $\textit{strs}$ 的行数为 $n$,列数为 $m$。 + +遍历每一列,从第二行开始,逐列比较当前行和上一行的字符,如果当前行的字符小于上一行的字符,说明当前列不是按字典序非严格递增排列的,需要删除,结果加一,然后跳出内层循环。 + +最后返回结果即可。 + +时间复杂度 $O(L)$,其中 $L$ 为字符串数组 $\textit{strs}$ 的长度。空间复杂度 $O(1)$。 @@ -129,18 +137,17 @@ class Solution { class Solution { public: int minDeletionSize(vector& strs) { - int n = strs.size(); - int m = strs[0].size(); - int res = 0; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n - 1; ++j) { - if (strs[j][i] > strs[j + 1][i]) { - res++; + int m = strs[0].size(), n = strs.size(); + int ans = 0; + for (int j = 0; j < m; ++j) { + for (int i = 1; i < n; ++i) { + if (strs[i][j] < strs[i - 1][j]) { + ++ans; break; } } } - return res; + return ans; } }; ``` @@ -148,9 +155,8 @@ public: #### Go ```go -func minDeletionSize(strs []string) int { +func minDeletionSize(strs []string) (ans int) { m, n := len(strs[0]), len(strs) - ans := 0 for j := 0; j < m; j++ { for i := 1; i < n; i++ { if strs[i][j] < strs[i-1][j] { @@ -159,7 +165,25 @@ func minDeletionSize(strs []string) int { } } } - return ans + return +} +``` + +#### TypeScript + +```ts +function minDeletionSize(strs: string[]): number { + const [m, n] = [strs[0].length, strs.length]; + let ans = 0; + for (let j = 0; j < m; ++j) { + for (let i = 1; i < n; ++i) { + if (strs[i][j] < strs[i - 1][j]) { + ++ans; + break; + } + } + } + return ans; } ``` @@ -170,16 +194,16 @@ impl Solution { pub fn min_deletion_size(strs: Vec) -> i32 { let n = strs.len(); let m = strs[0].len(); - let mut res = 0; - for i in 0..m { - for j in 1..n { - if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] { - res += 1; + let mut ans = 0; + for j in 0..m { + for i in 1..n { + if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] { + ans += 1; break; } } } - res + ans } } ``` diff --git a/solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md b/solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md index 3ac620db5d869..de846637ceb44 100644 --- a/solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md +++ b/solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md @@ -87,7 +87,15 @@ All 3 columns are not sorted, so you will delete all 3. -### Solution 1 +### Solution 1: Compare Column by Column + +We denote the number of rows in the string array $\textit{strs}$ as $n$, and the number of columns as $m$. + +We traverse each column, starting from the second row, and compare the character of the current row with that of the previous row column by column. If the character of the current row is less than that of the previous row, it indicates that the current column is not arranged in non-strictly increasing lexicographical order, and we need to delete it, incrementing the result by one, then break out of the inner loop. + +Finally, we return the result. + +The time complexity is $O(L)$, where $L$ is the total length of the strings in the array $\textit{strs}$. The space complexity is $O(1)$. @@ -132,18 +140,17 @@ class Solution { class Solution { public: int minDeletionSize(vector& strs) { - int n = strs.size(); - int m = strs[0].size(); - int res = 0; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n - 1; ++j) { - if (strs[j][i] > strs[j + 1][i]) { - res++; + int m = strs[0].size(), n = strs.size(); + int ans = 0; + for (int j = 0; j < m; ++j) { + for (int i = 1; i < n; ++i) { + if (strs[i][j] < strs[i - 1][j]) { + ++ans; break; } } } - return res; + return ans; } }; ``` @@ -151,9 +158,8 @@ public: #### Go ```go -func minDeletionSize(strs []string) int { +func minDeletionSize(strs []string) (ans int) { m, n := len(strs[0]), len(strs) - ans := 0 for j := 0; j < m; j++ { for i := 1; i < n; i++ { if strs[i][j] < strs[i-1][j] { @@ -162,7 +168,25 @@ func minDeletionSize(strs []string) int { } } } - return ans + return +} +``` + +#### TypeScript + +```ts +function minDeletionSize(strs: string[]): number { + const [m, n] = [strs[0].length, strs.length]; + let ans = 0; + for (let j = 0; j < m; ++j) { + for (let i = 1; i < n; ++i) { + if (strs[i][j] < strs[i - 1][j]) { + ++ans; + break; + } + } + } + return ans; } ``` @@ -173,16 +197,16 @@ impl Solution { pub fn min_deletion_size(strs: Vec) -> i32 { let n = strs.len(); let m = strs[0].len(); - let mut res = 0; - for i in 0..m { - for j in 1..n { - if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] { - res += 1; + let mut ans = 0; + for j in 0..m { + for i in 1..n { + if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] { + ans += 1; break; } } } - res + ans } } ``` diff --git a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.cpp b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.cpp index f54f5f6cac1ba..099d4b6ba9f86 100644 --- a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.cpp +++ b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.cpp @@ -1,17 +1,16 @@ class Solution { public: int minDeletionSize(vector& strs) { - int n = strs.size(); - int m = strs[0].size(); - int res = 0; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n - 1; ++j) { - if (strs[j][i] > strs[j + 1][i]) { - res++; + int m = strs[0].size(), n = strs.size(); + int ans = 0; + for (int j = 0; j < m; ++j) { + for (int i = 1; i < n; ++i) { + if (strs[i][j] < strs[i - 1][j]) { + ++ans; break; } } } - return res; + return ans; } }; \ No newline at end of file diff --git a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.go b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.go index 51e954878ba34..7de5c18a52179 100644 --- a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.go +++ b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.go @@ -1,6 +1,5 @@ -func minDeletionSize(strs []string) int { +func minDeletionSize(strs []string) (ans int) { m, n := len(strs[0]), len(strs) - ans := 0 for j := 0; j < m; j++ { for i := 1; i < n; i++ { if strs[i][j] < strs[i-1][j] { @@ -9,5 +8,5 @@ func minDeletionSize(strs []string) int { } } } - return ans + return } \ No newline at end of file diff --git a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.rs b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.rs index a50b68828cc39..c66bf2650b79e 100644 --- a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.rs +++ b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.rs @@ -2,15 +2,15 @@ impl Solution { pub fn min_deletion_size(strs: Vec) -> i32 { let n = strs.len(); let m = strs[0].len(); - let mut res = 0; - for i in 0..m { - for j in 1..n { - if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] { - res += 1; + let mut ans = 0; + for j in 0..m { + for i in 1..n { + if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] { + ans += 1; break; } } } - res + ans } } diff --git a/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.ts b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.ts new file mode 100644 index 0000000000000..d9c5a3a8849ec --- /dev/null +++ b/solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.ts @@ -0,0 +1,13 @@ +function minDeletionSize(strs: string[]): number { + const [m, n] = [strs[0].length, strs.length]; + let ans = 0; + for (let j = 0; j < m; ++j) { + for (let i = 1; i < n; ++i) { + if (strs[i][j] < strs[i - 1][j]) { + ++ans; + break; + } + } + } + return ans; +}