Skip to content

Commit 9cef036

Browse files
authored
feat: add solutions to lc problem: No.0944 (doocs#3211)
No.0944.Delete Columns to Make Sorted
1 parent bbb519f commit 9cef036

File tree

6 files changed

+112
-53
lines changed

6 files changed

+112
-53
lines changed

solution/0900-0999/0944.Delete Columns to Make Sorted/README.md

+42-18
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ cae</pre>
8484

8585
<!-- solution:start -->
8686

87-
### 方法一
87+
### 方法一:逐列比较
88+
89+
我们记字符串数组 $\textit{strs}$ 的行数为 $n$,列数为 $m$。
90+
91+
遍历每一列,从第二行开始,逐列比较当前行和上一行的字符,如果当前行的字符小于上一行的字符,说明当前列不是按字典序非严格递增排列的,需要删除,结果加一,然后跳出内层循环。
92+
93+
最后返回结果即可。
94+
95+
时间复杂度 $O(L)$,其中 $L$ 为字符串数组 $\textit{strs}$ 的长度。空间复杂度 $O(1)$。
8896

8997
<!-- tabs:start -->
9098

@@ -129,28 +137,26 @@ class Solution {
129137
class Solution {
130138
public:
131139
int minDeletionSize(vector<string>& strs) {
132-
int n = strs.size();
133-
int m = strs[0].size();
134-
int res = 0;
135-
for (int i = 0; i < m; ++i) {
136-
for (int j = 0; j < n - 1; ++j) {
137-
if (strs[j][i] > strs[j + 1][i]) {
138-
res++;
140+
int m = strs[0].size(), n = strs.size();
141+
int ans = 0;
142+
for (int j = 0; j < m; ++j) {
143+
for (int i = 1; i < n; ++i) {
144+
if (strs[i][j] < strs[i - 1][j]) {
145+
++ans;
139146
break;
140147
}
141148
}
142149
}
143-
return res;
150+
return ans;
144151
}
145152
};
146153
```
147154
148155
#### Go
149156
150157
```go
151-
func minDeletionSize(strs []string) int {
158+
func minDeletionSize(strs []string) (ans int) {
152159
m, n := len(strs[0]), len(strs)
153-
ans := 0
154160
for j := 0; j < m; j++ {
155161
for i := 1; i < n; i++ {
156162
if strs[i][j] < strs[i-1][j] {
@@ -159,7 +165,25 @@ func minDeletionSize(strs []string) int {
159165
}
160166
}
161167
}
162-
return ans
168+
return
169+
}
170+
```
171+
172+
#### TypeScript
173+
174+
```ts
175+
function minDeletionSize(strs: string[]): number {
176+
const [m, n] = [strs[0].length, strs.length];
177+
let ans = 0;
178+
for (let j = 0; j < m; ++j) {
179+
for (let i = 1; i < n; ++i) {
180+
if (strs[i][j] < strs[i - 1][j]) {
181+
++ans;
182+
break;
183+
}
184+
}
185+
}
186+
return ans;
163187
}
164188
```
165189

@@ -170,16 +194,16 @@ impl Solution {
170194
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
171195
let n = strs.len();
172196
let m = strs[0].len();
173-
let mut res = 0;
174-
for i in 0..m {
175-
for j in 1..n {
176-
if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] {
177-
res += 1;
197+
let mut ans = 0;
198+
for j in 0..m {
199+
for i in 1..n {
200+
if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] {
201+
ans += 1;
178202
break;
179203
}
180204
}
181205
}
182-
res
206+
ans
183207
}
184208
}
185209
```

solution/0900-0999/0944.Delete Columns to Make Sorted/README_EN.md

+42-18
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ All 3 columns are not sorted, so you will delete all 3.
8787

8888
<!-- solution:start -->
8989

90-
### Solution 1
90+
### Solution 1: Compare Column by Column
91+
92+
We denote the number of rows in the string array $\textit{strs}$ as $n$, and the number of columns as $m$.
93+
94+
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.
95+
96+
Finally, we return the result.
97+
98+
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)$.
9199

92100
<!-- tabs:start -->
93101

@@ -132,28 +140,26 @@ class Solution {
132140
class Solution {
133141
public:
134142
int minDeletionSize(vector<string>& strs) {
135-
int n = strs.size();
136-
int m = strs[0].size();
137-
int res = 0;
138-
for (int i = 0; i < m; ++i) {
139-
for (int j = 0; j < n - 1; ++j) {
140-
if (strs[j][i] > strs[j + 1][i]) {
141-
res++;
143+
int m = strs[0].size(), n = strs.size();
144+
int ans = 0;
145+
for (int j = 0; j < m; ++j) {
146+
for (int i = 1; i < n; ++i) {
147+
if (strs[i][j] < strs[i - 1][j]) {
148+
++ans;
142149
break;
143150
}
144151
}
145152
}
146-
return res;
153+
return ans;
147154
}
148155
};
149156
```
150157
151158
#### Go
152159
153160
```go
154-
func minDeletionSize(strs []string) int {
161+
func minDeletionSize(strs []string) (ans int) {
155162
m, n := len(strs[0]), len(strs)
156-
ans := 0
157163
for j := 0; j < m; j++ {
158164
for i := 1; i < n; i++ {
159165
if strs[i][j] < strs[i-1][j] {
@@ -162,7 +168,25 @@ func minDeletionSize(strs []string) int {
162168
}
163169
}
164170
}
165-
return ans
171+
return
172+
}
173+
```
174+
175+
#### TypeScript
176+
177+
```ts
178+
function minDeletionSize(strs: string[]): number {
179+
const [m, n] = [strs[0].length, strs.length];
180+
let ans = 0;
181+
for (let j = 0; j < m; ++j) {
182+
for (let i = 1; i < n; ++i) {
183+
if (strs[i][j] < strs[i - 1][j]) {
184+
++ans;
185+
break;
186+
}
187+
}
188+
}
189+
return ans;
166190
}
167191
```
168192

@@ -173,16 +197,16 @@ impl Solution {
173197
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
174198
let n = strs.len();
175199
let m = strs[0].len();
176-
let mut res = 0;
177-
for i in 0..m {
178-
for j in 1..n {
179-
if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] {
180-
res += 1;
200+
let mut ans = 0;
201+
for j in 0..m {
202+
for i in 1..n {
203+
if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] {
204+
ans += 1;
181205
break;
182206
}
183207
}
184208
}
185-
res
209+
ans
186210
}
187211
}
188212
```
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
class Solution {
22
public:
33
int minDeletionSize(vector<string>& strs) {
4-
int n = strs.size();
5-
int m = strs[0].size();
6-
int res = 0;
7-
for (int i = 0; i < m; ++i) {
8-
for (int j = 0; j < n - 1; ++j) {
9-
if (strs[j][i] > strs[j + 1][i]) {
10-
res++;
4+
int m = strs[0].size(), n = strs.size();
5+
int ans = 0;
6+
for (int j = 0; j < m; ++j) {
7+
for (int i = 1; i < n; ++i) {
8+
if (strs[i][j] < strs[i - 1][j]) {
9+
++ans;
1110
break;
1211
}
1312
}
1413
}
15-
return res;
14+
return ans;
1615
}
1716
};
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
func minDeletionSize(strs []string) int {
1+
func minDeletionSize(strs []string) (ans int) {
22
m, n := len(strs[0]), len(strs)
3-
ans := 0
43
for j := 0; j < m; j++ {
54
for i := 1; i < n; i++ {
65
if strs[i][j] < strs[i-1][j] {
@@ -9,5 +8,5 @@ func minDeletionSize(strs []string) int {
98
}
109
}
1110
}
12-
return ans
11+
return
1312
}

solution/0900-0999/0944.Delete Columns to Make Sorted/Solution.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ impl Solution {
22
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
33
let n = strs.len();
44
let m = strs[0].len();
5-
let mut res = 0;
6-
for i in 0..m {
7-
for j in 1..n {
8-
if strs[j - 1].as_bytes()[i] > strs[j].as_bytes()[i] {
9-
res += 1;
5+
let mut ans = 0;
6+
for j in 0..m {
7+
for i in 1..n {
8+
if strs[i].as_bytes()[j] < strs[i - 1].as_bytes()[j] {
9+
ans += 1;
1010
break;
1111
}
1212
}
1313
}
14-
res
14+
ans
1515
}
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minDeletionSize(strs: string[]): number {
2+
const [m, n] = [strs[0].length, strs.length];
3+
let ans = 0;
4+
for (let j = 0; j < m; ++j) {
5+
for (let i = 1; i < n; ++i) {
6+
if (strs[i][j] < strs[i - 1][j]) {
7+
++ans;
8+
break;
9+
}
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)